2015-09-26 69 views
-2

我正在使用python類進行編程的介紹,我們的作業是做99瓶啤酒歌曲。我們還沒有學到了很多東西又是那麼這的確是所有我能想出:試圖在python中編碼99瓶啤酒

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

你也需要測試它... StandardVerse()缺少參數,應該是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

你需要傳遞nStandardVerse,N只能是等於一個數量在同一時間,以便使用if /伊利斯的和其他人,不打印你的函數調用的默認情況下,還得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或等於W就必須在你的範圍內的情況下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()