首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让十个绿色的瓶子歌唱

让十个绿色的瓶子歌唱
EN

Stack Overflow用户
提问于 2017-09-28 00:40:44
回答 2查看 2.6K关注 0票数 0
代码语言:javascript
复制
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")

这是我的代码,但它不能工作,有人能帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2017-09-28 00:43:00

代码语言:javascript
复制
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‘打印为变量。

票数 2
EN

Stack Overflow用户

发布于 2017-09-28 00:57:41

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

代码语言:javascript
复制
for x in range(10,0,-1):
  #your while block here

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46453099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档