简体   繁体   中英

Is there a way to write FOR loop INSIDE of PRINT statement?

The homework given to me by my teacher is to display 'The beer song' in one line of code, using python. The song is here: http://www.99-bottles-of-beer.net/lyrics.html

I'm using python 3.7 and running it on Pycharm. I have got it in 2 lines, but the teacher insists, it can be done in one.

I'm sorry if its difficult to read. The code I wrote is as follows:

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"))))

Use a list comprehension and join to generate the entire text in one line:

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

Output:

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.

Yes there is. List comprehension allows you to use a for loop to create a list. It would look like this: (sorry for not solving it entirely, can't be bothered)

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

For example:

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

will return

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

Edit: To achieve that each element is printed on its own line, use

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

Thanks to @Mark Tolonen and that last edit by @LEEE, i completed the solution without any grammatical mistakes.

The initial lines of code simply print what was required and asked for,

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

but adding this if else prints it without the "1 bottles of beer" grammatical error,

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

Believe me, this is all in 1 line!!! I'm so happy!

The output:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM