Ошибка синтаксиса Python 3.6 elif - PullRequest
0 голосов
/ 03 мая 2018

с использованием вложенного оператора if и моего отступа кажется правильным, но все же восстанавливает синтаксическую ошибку. Спасибо

# FIGHT Dragons
if ch3 in ['y', 'Y', 'Yes', 'YES', 'yes']:

    # WITH SWORD
    if sword == 1:
        print ("You only have a sword to fight with!")
        print ("You quickly jab the Dragon in it's chest and gain an advantage")
        time.sleep(2)
        print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print ("                  Fighting...                   ")
        print ("   YOU MUST HIT ABOVE A 5 TO KILL THE DRAGON    ")
        print ("IF THE DRAGON HITS HIGHER THAN YOU, YOU WILL DIE")
        print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        time.sleep(2)
        fdmg1 = int(random.randint(3, 10))
        edmg1 = int(random.randint(1, 5))
        print ("you hit a", fdmg1)
        print ("the dragon hits a", edmg1)
        time.sleep(2)

        if edmg1 > fdmg1:
            print ("The drgon has dealt more damage than you!")
            complete = 0
        return complete 

Здесь я сталкиваюсь с синтаксической ошибкой

        elif fdmg1 < 5:
            print ("You didn't do enough damage to kill the drgon, but you manage to escape")
            complete = 1
        return complete 
        else:
            print ("You killed the drgon!")
            complete = 1
        return complete 

1 Ответ

0 голосов
/ 03 мая 2018

Ваше возвращение должно быть в конце оператора if ... elif ... else. Это работает:

if edmg1 > fdmg1:
    print ("The drgon has dealt more damage than you!")
    complete = 0

elif fdmg1 < 5:
    print ("You didn't do enough damage to kill the drgon, but you manage to escape")
    complete = 1

else:
    print ("You killed the drgon!")
    complete = 1

return complete   

Обратите внимание, что если первым условием if является True, Python не будет проверять последующие.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...