繁体   English   中英

制作十个绿瓶歌曲

[英]Making the ten green bottles song

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 = 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 loop' 应该允许程序在 x 大于 0 时循环播放歌曲。歌曲将求和 x = x - 1 并将新的 'x' 作为变量打印。

您的while块不会由于条件未失败而终止(如果您没有正确初始化 x,则不满足)。 对于这种类型的用法,我建议使用for循环而不是while来确保您的代码将终止。 例如

for x in range(10,0,-1):
  #your while block here

range函数将创建一个类似列表的对象,从 10 到 1,倒计时。 for循环将遍历所有元素并在完成时终止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM