首页
学习
活动
专区
工具
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上运行它。我只用了两行就完成了,但老师坚持说,一行就能完成。

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

代码语言:javascript
复制
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

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-23 23:37:22

使用列表理解和join在一行中生成整个文本:

代码语言:javascript
复制
print('\n\n'.join([f'{i} bottle{"" if i==1 else "s"} of beer on the wall, {i} bottle{"" if i==1 else "s"} of beer.\nTake one down and pass it around, {"no" if i==1 else i-1} bottle{"" if i-1==1 else "s"} of beer on the wall.' for i in range(99,0,-1)]) + '\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.')

输出:

代码语言:javascript
复制
99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.

...

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no bottles of beer on the wall.

No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
票数 1
EN

Stack Overflow用户

发布于 2019-06-24 00:30:58

感谢@Mark Tolonen和@LEEE的最后一次编辑,我完成了解决方案,没有任何语法错误。

最初的几行代码只是简单地打印所需的内容,

代码语言:javascript
复制
print('\n'.join(f'{i} bottles of beer on the wall, {i} bottles of beer...
\nTake one down and pass it around, {i-1} bottles of beer on the wall.'

但是如果加上这个if则会打印出没有"1瓶啤酒“的语法错误,

代码语言:javascript
复制
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 ("1 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 ("No 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."))) 

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

相信我,这一切都在一行!我高兴极了!

输出:

代码语言:javascript
复制
99 bottles of beer on the wall, 99 bottles of beer...
Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer...
Take one down and pass it around, 97 bottles of beer on the wall.
...
2 bottles of beer on the wall, 2 bottles of beer on the wall...
Take one down and pass it around, 1 more bottle of beer.
1 bottle of beer on the wall, 1 bottle of beer on the wall...
Take one down and pass it around, no more bottles of beer.
No more bottles of beer on the wall, no bottles of beer on the wall...
Go to the shop and buy some more, 99 more bottles of beer.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56725338

复制
相关文章

相似问题

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