为什么即使猜测正确的字符串也要求我输入字符串

时间:2018-11-24 01:16:15

标签: python python-3.x loops while-loop

secret_word = "giraffe"
guess = ""

while guess != secret_word:
    guess = input("Enter your guess: ")

print("Bravo, you've guessed it right!")

1 个答案:

答案 0 :(得分:0)

您的原始代码在python上为我工作。您可以尝试摆脱前导空格和尾随空格的影响,使其更强大。

secret_word = "giraffe"
guess = ""

while guess.strip() != secret_word:
    guess = input("Enter your guess: ")

print("Bravo, you've guessed it right!")

如果您使用的是Python 2解释器,由于语法更改,它将无法正常工作;因此,您可以将代码编辑为与Python 2兼容。或者您可能在命令行中调用了错误的终端,以确保它是python 3,您通常可以执行python3 my_program.py来显式使用python 3。

secret_word = "giraffe"
guess = ""

while guess != secret_word:
    guess = raw_input("Enter your guess: ")

print "Bravo, you've guessed it right!"

干杯!