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")这是我的代码,但它不能工作,有人能帮我吗?
发布于 2017-09-28 00:43:00
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‘打印为变量。
发布于 2017-09-28 00:57:41
您的while块不会因为条件没有失败而终止(或者,如果您没有正确初始化您的x,则不会满足)。对于这种类型的使用,我建议使用for循环而不是while,以确保您的代码将终止。例如:
for x in range(10,0,-1):
#your while block hererange函数将创建一个从10到1的列表状对象,并进行倒计时。for循环将遍历所有元素,并在完成时终止。
https://stackoverflow.com/questions/46453099
复制相似问题