while循环有两个输入调用

时间:2017-10-09 01:46:29

标签: python while-loop

我正在学习Python,我遇到了while循环问题。我在下面有一个示例代码,它包含一个while循环。我想在这里做的是打印“哈!你永远不会猜到“每次没有预测到正确的名字,但我也希望代码打印”不!你怎么猜?“当预测正确的名字时。我一直在玩它,它确实做我想要的但是系统我必须测试我的代码它说它是不正确的并且我的程序试图读取比它应该需要的更多输入。任何帮助将不胜感激!

print("You will never win the game, for Scooby dooby doo! is my name.")
rumple = input("Guess my name: ")

while rumple != "Rumplestiltskin":

        print("Ha! You'll never guess!")
        rumple = input("Guess my name: ")



print('No! How did you guess?')

2 个答案:

答案 0 :(得分:0)

你使用哪种系统?在我更正了缩进后,我测试了你的代码,我没有收到任何错误或警告。

print("You will never win the game, for Scooby dooby doo! is my name.")
rumple = input("Guess my name: ")

while rumple != "Rumplestiltskin":
    print("Ha! You'll never guess!")
    rumple = input("Guess my name: ")

print('No! How did you guess?')

我的结果:

You will never win the game, for Scooby dooby doo! is my name.
Guess my name: Scooby
Ha! You'll never guess!
Guess my name: Rumplestiltskin
No! How did you guess?

Process finished with exit code 0

答案 1 :(得分:0)

假设任务只有一个input功能,您可以将while循环更改为

while "Rumplestiltskin" != input("Guess my name: "):
    print("Ha! You'll never guess!")

print('No! How did you guess?')

这会将input函数放入while循环中,而不需要事先初始化rumple变量。