2015-09-26 5 views
-2

Я нахожусь в программировании с использованием класса python, и наша домашняя работа - сделать 99 бутылок пивной песни. Мы еще не научились много еще, так что это действительно все, что я могу придумать:Пытаясь ввести 99 бутылок пива в python

def StandardVerse(): 
    print n, "bottles of beer on the wall,", n, ",bottles of beer" 
    print "take one down pass it around,",n,"bottles of beer on the wall." 

def TwoBottles(): 
    print "Two bottles of beer on the wall, two bottles of beer." 
    print "Take one down pass it around, one bottle of beer on the wall." 

def OneBottle(): 
    print "One bottle of beer on the wall, One bottle of beer." 
    print "Take one down, pass it around, no more bottles of beer on the wall." 

def NoBottles(): 
    print "No more bottles of beer on the wall, no more bottles of beer." 
    print "Go to the store, buy some more, 99 bottles of beer on the wall." 

for n in range(99,0,-1): 
    if n > 2: 
     print StandardVerse 
    if n == 2: 
     print TwoBottles 
    if n == 1: 
     print OneBottle 
    if n <= 1: 
     print NoBottles 

Это дает мне это, когда я запустить его

<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function StandardVerse at 0x027BEC30> 
<function TwoBottles at 0x027BEC70> 
<function OneBottle at 0x027BECB0> 
<function NoBottles at 0x027BECF0> 

и так далее до 99 бутылок (Я не копировал все это ради пространства.)

Что я могу сделать, чтобы распечатать эту песню?

+0

Если вы собираетесь печатать функцию вызовов затем возвращать строки или еще не pting просто позвонить –

ответ

0

использование StandardVerse() и т. Д. Таким образом вы вызываете функцию, и отпечатки будут выполнены.

-3

здесь вы идете:

for n in range(99,0,-1): 
    if n > 2: 
     StandardVerse() 
    if n == 2: 
     TwoBottles() 
    if n == 1: 
     OneBottle() 
    if n <= 1: 
     NoBottles() 
+0

почему я получаю -1. Протестируйте код, прежде чем вы подумаете, что с ним что-то не так – matoliki

+0

Вам также нужно проверить его ... СтандартVerse() отсутствует параметр, должен быть StandardVerse ('n') – Max

+1

Технически,' n' является глобальным и реализуется выше должно работать (хотя 'n' должен быть параметром функции). – Alexander

0

Вы получаете None между строками, потому что вы print ИНГАМИ возвращаемых значений функций, которые явно не возвращают ничего (и так, по сути, они возвращают None).

Также необходимо пройти n до StandardVerse. Это будет работать:

for n in range(99, 0, -1): 
    if n > 2: 
     StandardVerse(n) 
    elif n == 2: 
     TwoBottles() 
    elif n == 1: 
     OneBottle() 
    else:    # n == 0 
     NoBottles() 
0

Нет необходимости в дополнительном вызове print(). Просто вызов функции должен работать:

for n in range(99,0,-1): 
    if n > 2: 
     StandardVerse(n) 
1

Вам необходимо пройти n к StandardVerse, п может быть равен одному номеру только в то время, поэтому следует использовать, если/ЭЛИС-х и еще, не печатать ваши вызовы функции, как не возвращает значения так None по умолчанию, которые вы увидите, если вы печатаете:

def StandardVerse(n): 
    print n, "bottles of beer on the wall,", n, ",bottles of beer" 
    print "take one down pass it around,",n,"bottles of beer on the wall." 

def TwoBottles(): 
    print "Two bottles of beer on the wall, two bottles of beer." 
    print "Take one down pass it around, one bottle of beer on the wall." 

def OneBottle(): 
    print "One bottle of beer on the wall, One bottle of beer." 
    print "Take one down, pass it around, no more bottles of beer on the wall." 

def NoBottles(): 
    print "No more bottles of beer on the wall, no more bottles of beer." 
    print "Go to the store, buy some more, 99 bottles of beer on the wall." 

for n in range(99,0,-1): 
    if n > 2: 
     StandardVerse(n) 
    elif n == 2: 
     TwoBottles() 
    elif n == 1: 
     OneBottle() 
    else: 
     NoBottles() 

Если n не> 2, равно 1 или равно ж она должна быть в контексте вашего диапазона 0.

0

Вы можете сделать это:

for i in range(99, -1, -1): 
    if i > 2: 
     print ('{} bottles of beer on the wall!\n{} bottles of beer!\nTake one down\nAnd pass it around\n{} bottles of beer on the wall!\n\n'.format (i,i,i-1)) 
    elif i == 2: 
     print ('{} bottles of beer on the wall!\n{} bottles of beer!\nTake one down\nAnd pass it around\n1 more bottle of beer on the wall!\n\n'.format (i,i,)) 
    elif i == 1: 
     print ('1 bottle of beer on the wall!\n1 bottle of beer!\nTake it down\nAnd pass it around\nNo more bottles of beer on the wall!') 
print() 
Смежные вопросы