老师给我的作业是用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"))))
发布于 2019-06-23 23:37:22
使用列表理解和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.
发布于 2019-06-24 00:30:58
感谢@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.'
但是如果加上这个if则会打印出没有"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)))
相信我,这一切都在一行!我高兴极了!
输出:
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.
https://stackoverflow.com/questions/56725338
复制相似问题