2017-09-27 27 views
0
while x > 0: 
    print (str(x) + "green bottles hanging on the wall") 
    print (str(x) + "green bottles hanging on the wall") 
    print ("if one green bottle accidently falls") 

這是我的代碼,但它不起作用,請問有人能幫我嗎?製作十首綠色的瓶子歌曲

+1

究竟是什麼問題?你有錯誤嗎?錯誤的輸出? – Mureinik

+0

'while x> 0''但是然後'x'永遠不會改變,爲什麼循環終止? – CoryKramer

回答

2
x = 10 
while x > 0: 
    print (str(x) + "green bottles hanging on the wall") 
    print (str(x) + "green bottles hanging on the wall") 
    print ("if one green bottle accidently falls") 
    x = x - 1 
    print ("there will be " + str (x) + "hanging on the wall") 

嘗試添加到您的代碼。 'While循環'應該允許程序循環播放歌曲,而x大於0,歌曲將執行和x = x - 1並將新的'x'作爲變量進行打印。

+0

另外你應該在「綠色」和「懸掛」單詞之前加一個額外的空格。 –

0

您的while由於條件沒有失敗(或者如果您沒有正確初始化x),塊不會終止。對於這種類型的用法,我建議使用for循環而不是while來確保您的代碼將終止。例如

​​

range功能將創建列表狀物體從10到1,向下計數。 for循環將迭代所有元素,並在完成時終止。

相關問題