有没有办法写PRINT语句的FOR循环?

时间:2019-06-23 15:31:17

标签: python python-3.x for-loop printing while-loop

老师给我的作业是使用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"))))

3 个答案:

答案 0 :(得分:2)

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

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.')

输出:

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 :(得分:0)

是的。列表理解使您可以使用ListView循环来创建列表。 它看起来像这样:(很抱歉没有完全解决它,不能打扰)

RowHeight

例如:

for

将返回

[<expression> for <element name> in <iterable>]

编辑:要实现每个元素都打印在自己的行上,请使用

[abs(i) for i in range(-5, 5)]

答案 2 :(得分:0)

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

代码的最初几行仅打印要求和要求的内容,

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.'

但是如果添加其他字体,则打印出来时不会出现“ 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 ("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)))

相信我,这一切都是1行!!!我真开心!

输出:

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.