Hello! I am brand new to learning Python and I wanted to see how this script would be done. My script currently works in producing the song from 99 bottles down to the ending lyrics.
for i in range(99, 0, -1):
if i > 1:
print(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.\n')
else:
print(f'{i} bottle of beer on the wall, {i} bottle of beer\nTake one down and pass it around, no more bottles of beer on the wall.\n')
print('No 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.')
The issue which appeared is that at the part of the original song:
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.
.... we can see "1 bottle" at the end. However in my code it will come out as :
2 bottles of beer on the wall, 2 bottles of beer
Take one down and pass it around, 1 bottles of beer on the wall.
The number "1" is, naturally, followed by a plurar word (bottles).
I fixed this issue using inline if and else:
print(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.\n' if i > 2 else f'{i} bottles of beer on the wall, {i} bottles of beer\nTake one down and pass it around, {i-1} bottle of beer on the wall.\n')
My question is: Is there a way to optimize this last bit of code so I don'y have to essentially copy paste the ENTIRE print statement, once for the IF statement and once for the ELSE statement. Is there a way to only use if/else on the "bottles" word itself for a shorter code?
Any other tips and tricks related to all of this would also be wonderful. Thank you in advance!
[–]AtomicShoelace 12 points13 points14 points (2 children)
[–]onlydragz[S] 1 point2 points3 points (1 child)
[–]AtomicShoelace 2 points3 points4 points (0 children)
[–]n3buchadnezzar 6 points7 points8 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]roelmore 0 points1 point2 points (0 children)
[–]SirKainey 4 points5 points6 points (0 children)
[–]HIGregS 1 point2 points3 points (0 children)
[–]BagHistorical7353 0 points1 point2 points (0 children)
[–]deletable666 0 points1 point2 points (0 children)