2015-09-26 65 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()