В то время как и если утверждения проблемы с моим скриптом? - PullRequest
1 голос
/ 08 июля 2020

Итак, я хочу напечатать «Неправильно!» каждый раз, когда я получаю неправильный ввод. Это работает, но когда я ввожу правильный ввод, он все равно выполняет оператор «if», несмотря на то, что «guess.capitalize ()! = Secret_word» ложно. Вы можете сказать мне, что я делаю не так? приветствуется использование нескольких методов!

secret_word = "Dog"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while guess.capitalize() != secret_word and not out_of_guesses:
    if guess_count < guess_limit:
        guess = input("Enter your guess: ")
        guess_count += 1
        print("Wrong!")
    else:
        out_of_guesses = True

if out_of_guesses:
    print("You ran out of guesses")
else:
    print("You won!")

Ответы [ 2 ]

0 голосов
/ 09 июля 2020

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

secret_word = "Dog"
guess_count = 0

while True:
    if guess_count != 3:
        guess_count +=1
        guess = input("Enter your guess: ") 
        if guess.upper() == secret_word.upper():
            print("You Won!")
            break
        else:
            print("Wrong")
            continue
    else:
        print("You ran out of guesses")
        break

пожалуйста, дайте мне знать, если это не результат вы пытаетесь получить.

0 голосов
/ 08 июля 2020

Я прокомментировал ту часть вашего кода, которая вызывает ошибку:

while guess.capitalize() != secret_word and not out_of_guesses:
    if guess_count < guess_limit:
        guess = input("Enter your guess: ")# Here, the user can input correct word or wrong word
        guess_count += 1
        print("Wrong!") # The code goes down here, regardless of what the user input
    else:
        out_of_guesses = True

Исправленная версия:

secret_word = "Dog"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while not out_of_guesses:
    if guess_count < guess_limit:
        guess = input("Enter your guess: ")# Here, the user can input correct word or wrong word
        if guess.lower() != secret_word.lower():
            guess_count += 1
            print("Wrong!") # The code goes down here, regardless of what the user input
        else:
            break
    else:
        out_of_guesses = True

if out_of_guesses:
    print("You ran out of guesses")
else:
    print("You won!")
...