2017-10-09 144 views
0

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

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?') 
+3

这看起来不错,虽然你有一些重复的代码(调用input()),你可以删除它。但是,您所描述的真正问题是“我必须测试我的代码的系统” - 这是我们需要查看哪些内容以确定哪些问题。 –

回答

0

假设任务是只有一个input功能,你可以在你的while循环改为

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

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

这会将你的input功能成while循环,而不需要事先初始化凸起变量。

+0

这似乎与问题中的代码行为相同。这是如何解决OP测试系统“试图阅读更多输入”的问题? –

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