首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有没有办法在PRINT语句中写FOR循环?

有没有办法在PRINT语句中写FOR循环?
EN

Stack Overflow用户
提问于 2019-06-23 23:31:18
回答 2查看 169关注 0票数 1

老师给我的作业是用python在一行代码中显示“啤酒之歌”。歌曲在这里:http://www.99-bottles-of-beer.net/lyrics.html

我正在使用python 3.7,并在Pycharm上运行它。我只用了两行就完成了,但老师坚持说,一行就能完成。

如果这篇文章很难读,我很抱歉。我写的代码如下:

for i in range(99, -1, -1):

        print("%d bottles of beer on the wall %d bottles of beer on the wall...\nTake one down and pass it around, %d bottles of beer\n" % (i, i, i - 1) if i > 2 else ("2 bottles of beer on the wall, 2 bottles of beer on the wall...\nTake one down and pass it around, 1 more bottle of beer" if i > 1 else ("\n1 bottle of beer on the wall, 1 bottle of beer on the wall...\nTake one down and pass it around, No more bottles of beer" if i>0 else ("\nNo more bottles of beer on the wall, no bottles of beer on the wall...\nGo to the shop and buy some more, 99 more bottles of beer"))))
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56725338

复制
相关文章

相似问题

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