Advertisement
horozov86

Man_O_War_without_func

Feb 15th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. pirate_ship = [int(x) for x in input().split(">")] # секциите на пиратския кораб като цели числа
  2. warship = [int(x) for x in input().split(">")] # секциите на бойния кораб като цели числа
  3. maximum_section_health = int(input()) # максималкия здравина/здраве на секция
  4.  
  5. flag = True
  6. while flag:
  7.     line = input()
  8.     if line == "Retire":
  9.         break
  10.  
  11.     command_split = line.split()
  12.     command = command_split[0] # извеждаме само командите, които са на индекс 0
  13.  
  14.     if command == "Fire":
  15.         idx = int(command_split[1]) # извеждаме текущия индекс като цяло число
  16.         damage = int(command_split[2]) # извеждаме текущия damage като цяло число
  17.         if not 0 <= idx < len(warship): # атакуваме част от warship под някакъв индек, който го проверяваме дали не е в ranga на warship, който е 0 до len(warship)
  18.             continue
  19.         warship[idx] -= damage # от секцията под този индекс на бпойния кораб изважадаме damage
  20.         if warship[idx] <= 0: # по условия, ако секцията стане <= 0, т.е. health <= 0 се принтира съобщения
  21.             flag = False
  22.             print(f"You won! The enemy ship has sunken.")
  23.  
  24.     elif command == "Defend": # разликата е, че се атакуват всички клетки. Трябва да се валидират и двата индекса, начален и краен
  25.         start_idx = int(command_split[1])
  26.         end_inx = int(command_split[2])
  27.         damage = int(command_split[3])
  28.         if not 0 <= start_idx < len(pirate_ship) or not 0 <= end_inx < len(pirate_ship):
  29.             continue
  30.         for idx in range(start_idx, end_inx + 1): # ако индекса е валиден циклим за всеки един сектор damage-a
  31.             pirate_ship[idx] -= damage
  32.             if pirate_ship[idx] <= 0: # ако в даден момент сектора стане <= 0 прекъсваме програмата и for цикъла
  33.                 print(f"You lost! The pirate ship has sunken.")
  34.                 flag = False
  35.                 break
  36.     elif command == "Repair": # поправя се определена секция, която е под индекс pirate_ship[idx]
  37.         idx = int(command_split[1])
  38.         health = int(command_split[2]) # добавя се health към съответната секция
  39.         if not 0 <= idx < len(pirate_ship):
  40.             continue
  41.         pirate_ship[idx] = min(maximum_section_health, pirate_ship[idx] + health) # взима се по-малкия health по условие
  42.  
  43.     elif command == "Status":
  44.         threshold = maximum_section_health * 0.2
  45.         counter = 0
  46.         for section in pirate_ship:
  47.             if section < threshold:
  48.                 counter += 1
  49.         print(f"{counter} sections need repair.")
  50.  
  51. if flag:
  52.     print(f"Pirate ship status: {sum(pirate_ship)}")
  53.     print(f"Warship status: {sum(warship)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement