【问题标题】:Is there a way to write FOR loop INSIDE of PRINT statement?有没有办法在 PRINT 语句的 FOR 循环 INSIDE 中编写?
【发布时间】:2019-11-05 14:20:32
【问题描述】:

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

【问题讨论】:

  • 为什么不把你想打印的东西赋值给一个变量,然后再打印这个变量
  • 您可以在 print 语句中使用列表推导并打印结果列表
  • 这本身是不可读的。我会再次将其分成多行。不知道为什么老师会问其他方式。
  • 你想查看列表推导
  • @Devesh Kumar Singh,正是我所做的。这就是为什么它带我两条线......

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


【解决方案1】:

使用列表推导和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.

【讨论】:

    【解决方案2】:

    感谢@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 else 打印它没有“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.
    

    【讨论】:

      【解决方案3】:

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

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

      例如:

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

      会回来

      [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]
      

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

      print("\n".join(<list>))
      

      【讨论】:

      • @Devesh 但他不会被打扰。
      • 我是抱歉。另外,是的,它确实回答了这个问题。他们问你是否可以在print 中使用for 语句,这可以通过列表理解@DeveshKumarSingh 来完成
      猜你喜欢
      • 1970-01-01
      • 2020-01-06
      • 2022-11-18
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      相关资源
      最近更新 更多