为什么它要求我输入一个字符串,即使猜到了正确的字符串

2024-05-29 04:28:05 发布

您现在位置:Python中文网/ 问答频道 /正文

secret_word = "giraffe"
guess = ""

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

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

Tags: rightyouinputyoursecretveitword
1条回答
网友
1楼 · 发布于 2024-05-29 04:28:05

你的原始代码在python上为我工作。您可以尝试去掉前导空格和尾随空格,使其更健壮

secret_word = "giraffe"
guess = ""

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

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

如果您使用的是python2解释器,那么由于语法的变化,它将无法正常工作;因此,您可以编辑代码使其与python2兼容。或者您可能在命令行中调用了错误的终端,以确保它是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!"

干杯

相关问题 更多 >

    热门问题