2017-10-09 69 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