Почему мой код пропускает запросы на ввод после второго? - PullRequest
0 голосов
/ 17 июня 2020

Я смотрел на это некоторое время, и любая помощь бесконечно приветствуется.

secret = "Meow"
guess = ""
limit = 1

while guess != secret and limit <= 5:
    if limit == 2:
        print("You have 4 guesses left!")
    elif limit == 3:
        print("You have 3 guesses left!")
    elif limit == 4:
        print("You have 2 guesses left!")
    elif limit == 5:
        print("You have 1 guess left!")

    if limit == 1:
        guess = input("What's the secret? OwO You have 5 guesses. ")
    elif limit <= 2:
        guess = input("What's the secret? OwO. ")

    limit += 1

print("You cheated! Or lost.")

При запуске он обычно делает попытку ввода и после неправильного ответа на вопрос, когда он говорит, что осталось 5 попыток и снова запрашивает ввод, но после этого пропускает вводную подсказку и говорит только догадки. Вывод с догадками:

What's the secret? OwO You have 5 guesses. idk man  
You have 4 guesses left!  
What's the secret? OwO. 2nd try?  
You have 3 guesses left!  
You have 2 guesses left!  
You have 1 guess left!  
You cheated! Or lost.  

Ответы [ 2 ]

2 голосов
/ 17 июня 2020

Это улучшение вашего кода

secret = "Meow"
guess = ""
limit = 1

while guess != secret and limit <= 5:

    if limit == 1:
        guess = input("What's the secret? OwO You have 5 guesses. ")
    elif limit >= 2: #here was your mistake
        print("You have {0} guess left!".format(5-limit+1))
        guess = input("What's the secret? OwO. ")

    limit += 1

print("You cheated! Or lost.")
2 голосов
/ 17 июня 2020

просто измените это:

elif limit >= 2:

secret = "Meow"
guess = ""
limit = 1

while guess != secret and limit <= 5:
    if limit == 2:
        print("You have 4 guesses left!")
    elif limit == 3:
        print("You have 3 guesses left!")
    elif limit == 4:
        print("You have 2 guesses left!")
    elif limit == 5:
        print("You have 1 guess left!")

    if limit == 1:
        guess = input("What's the secret? OwO You have 5 guesses. ")
    elif limit >= 2:
        guess = input("What's the secret? OwO. ")

    limit += 1

print("You cheated! Or lost.")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...