Я сделал программу для песни «99 бутылок», но она неправильно печатает песню

#python #python-3.x

Вопрос:

 def printLyrics(beer):  print(str(beer)   " bottles of beer on the wall, "   str(beer)   " bottles of beer")  print("Take one down and pass it around "   str(beer-1)   " bottles of beer on the wall.")  print()    if beer == 2:  print("2 bottles of beer on the wall, 2 bottles of beer.")  print("Take one down and pass it around, 1 bottle of beer on the wall.")  print()   elif beer == 1:  print("1 bottle of beer on the wall, 1 bottle of beer.")  print("Take one down and pass it around, no more bottles of beer on the wall.")  print()  

Эта строка печатается каждый раз, когда бутылки на стене бегут, я не знаю, как это исправить.
ещё: печать(«Больше никаких бутылок пива на стене, больше никаких бутылок пива».) печать(«Сходи в магазин и купи еще, 99 бутылок пива на стене»). печать()

def main(): для пива в диапазоне(99,0,-1): printLyrics(пиво)

главная()

Ответ №1:

Первый набор отпечатков должен быть условным для пива gt; 2.

 def printLyrics(beer):  if beergt;2:  print(str(beer)   " bottles of beer on the wall, "   str(beer)   " bottles of beer")  print("Take one down and pass it around "   str(beer-1)   " bottles of beer on the wall.")  print()  elif beer == 2:  print("2 bottles of beer on the wall, 2 bottles of beer.")  print("Take one down and pass it around, 1 bottle of beer on the wall.")  print()   else:  print("1 bottle of beer on the wall, 1 bottle of beer.")  print("Take one down and pass it around, no more bottles of beer on the wall.")  print()  for beer in range(99,0,-1):  printLyrics(beer)  

Ответ №2:

Я думал о том же, что и Ален.

 def print_lyrics(beer):  if beer == 2:  print("2 bottles of beer on the wall, 2 bottles of beer.")  print("Take one down and pass it around, 1 bottle of beer on the wall.")  print()   elif beer == 1:  print("1 bottle of beer on the wall, 1 bottle of beer.")  print("Take one down and pass it around, no more bottles of beer on the wall.")  print()  elif beer == 0:  print("No more bottles of beer on the wall, no more bottles of beer.")  print("Go to the store and buy some more, 99 bottles of beer on the wall.")  print()  else:  print(str(beer)   " bottles of beer on the wall, "   str(beer)   " bottles of beer")  print("Take one down and pass it around "   str(beer-1)   " bottles of beer on the wall.")  print()   for beer in range(99, -1, -1):  print_lyrics(beer)