Loading...
Krisi57 avatar Krisi57 0 Точки

Man O War - Къде съм сгрешил? Моля? Гърми на 7 и 8 тест!

def fire(index, damage, warship):
    warship[index] -= damage

    if warship[index] <= 0:
        return True


def defend(start_index, end_index, damage, pirate_ship):
    for i in range(start_index, end_index + 1):
        pirate_ship[i] -= damage
        if pirate_ship[i] <= 0:
            return True


def repair(index, health, pirate_ship, health_capacity):
    pirate_ship[index] += health
    if pirate_ship[index] > health_capacity:
        pirate_ship[index] -= pirate_ship[index] - health_capacity


def status(pirate_ship, health_capacity):
    count = 0
    for section in pirate_ship:
        if section < 0.2 * health_capacity:
            count += 1
    print(f"{count} sections need repair.")


pirate_ship = list(map(int, input().split(">")))
warship = list(map(int, input().split(">")))
max_health_capacity = int(input())
winner = False

while True:
    command = input().split()
    if command[0] == "Retire":
        break
    elif command[0] == "Fire":
        if 0 <= int(command[1]) <= len(warship) - 1:
            winner = fire(int(command[1]), int(command[2]), warship)
            if winner:
                print("You win! The enemy ship has sunken.")
                break
    elif command[0] == "Defend":
        if 0 <= int(command[1]) <= len(pirate_ship) - 1 and 0 <= int(command[2]) <= len(pirate_ship) - 1:
            winner = defend(int(command[1]), int(command[2]), int(command[3]), pirate_ship)
            if winner:
                print("You lost! The pirate ship has sunken.")
                break
    elif command[0] == "Repair":
        if 0 <= int(command[1]) <= len(pirate_ship) - 1:
            repair(int(command[1]), int(command[2]), pirate_ship, max_health_capacity)
    elif command[0] == "Status":
        status(pirate_ship, max_health_capacity)

if not winner:
    print(f"Pirate ship status: {sum(pirate_ship)}")
    print(f"Warship status: {sum(warship)}")
Тагове:
0
Fundamentals Module 27/08/2020 00:15:21
valerielashvili avatar valerielashvili 34 Точки

Моето решение без функции:

pirate_ship = [int(x) for x in input().split('>')]
warship = [int(x) for x in input().split('>')]
max_health = int(input())

while True:
    tokens = input().split()
    command = tokens[0]

    if command == 'Retire':
        break

    if command == 'Fire':
        i, damage = int(tokens[1]), int(tokens[2])

        if 0 <= i < len(warship):
            warship[i] -= damage

            if warship[i] <= 0:
                print("You won! The enemy ship has sunken.")
                break

    elif command == 'Defend':
        start_i, end_i, damage = int(tokens[1]), int(tokens[2]), int(tokens[3])
        sunk = False

        if 0 <= start_i < len(pirate_ship) and 0 <= end_i < len(pirate_ship):
            for idx in range(start_i, end_i + 1):
                pirate_ship[idx] -= damage

                if pirate_ship[idx] <= 0:
                    print("You lost! The pirate ship has sunken.")
                    sunk = True
                    break
        if sunk:
            break

    elif command == 'Repair':
        i, health = int(tokens[1]), int(tokens[2])

        if 0 <= i < len(pirate_ship):
            if pirate_ship[i] + health <= max_health:
                pirate_ship[i] += health
            else:
                pirate_ship[i] = max_health

    elif command == 'Status':
        repair_limit = max_health * 0.2
        sections_req_repair = sum(1 for x in pirate_ship if x < repair_limit)
        print(f"{sections_req_repair} sections need repair.")

if all(x > 0 for x in pirate_ship) and all(x > 0 for x in warship):
    print(f"Pirate ship status: {sum(s for s in pirate_ship)}\n"
          f"Warship status: {sum(s for s in warship)}")

 

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.