试图在python中编码99瓶啤酒

时间:2015-09-26 18:13:20

标签: python

我正在使用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瓶(我没有为了空间而复制它们。)

我该怎么做才能打印出真正的歌曲?

6 个答案:

答案 0 :(得分:2)

你需要将n传递给StandardVerse,n一次只能等于一个数字所以使用if / elis's,否则,不要打印你的函数调用作为返回没有值默认情况下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不是&gt; 2,等于1或等于w,在你的范围内必须是0

答案 1 :(得分:1)

你在行之间得到None,因为你打印的函数的返回值没有显式返回任何东西(事实上,它们返回None)。 此外,您需要将n传递给StandardVerse。这将有效: 对于范围(99,0,-1)中的n:     如果n> 2:         StandardVerse(N)     elif n == 2:         TwoBottles()     elif n == 1:         OneBottle()     否则:#n == 0         NoBottles()

答案 2 :(得分:0)

使用StandardVerse()等。这样您就可以调用该函数并执行打印。

答案 3 :(得分:0)

无需额外的print()调用。只需调用该函数即可: 对于范围内的n(99,0,-1):     如果n> 2:         StandardVerse(N)

答案 4 :(得分: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()

答案 5 :(得分:-3)

你去:

REF = structure(list(chr = c("1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), 
    start = c(766101L, 1627918L, 4421060L, 7324468L, 8053732L, 
    8182462L, 8182584L, 8182584L, 8206130L, 8804237L, 10369546L, 
    10370541L, 10543836L, 10656324L, 12354307L, 12841928L, 12845863L, 
    12909237L, 12909965L, 13444908L), end = c(809773L, 1672603L, 
    4424115L, 7325408L, 8067990L, 8189854L, 8189285L, 8189285L, 
    8209321L, 8812660L, 10377983L, 10377983L, 10545046L, 10657912L, 
    12357076L, 12971833L, 12883096L, 12927107L, 12918079L, 13468022L
    ), Deft = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1)), .Names = c("chr", "start", "end", "Deft"
), row.names = c(217L, 568L, 1340L, 1691L, 1804L, 1811L, 1812L, 
1813L, 1819L, 1880L, 2017L, 2020L, 2041L, 2049L, 2224L, 2282L, 
2284L, 2332L, 2335L, 2424L), class = "data.frame")

TEST = structure(list(sampleID = c("SID1331", "SID1331", "SID1331", 
"SID1331", "SID1331", "SID1331", "SID1331", "SID1331", "SID1331", 
"SID1331", "SID1337", "SID1337", "SID1337", "SID1337", "SID1337", 
"SID1337", "SID1337", "SID1337", "SID1337", "SID1337", "SID1337"
), Chromosome = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 
10L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L), Start = c(61735L, 
7541291L, 32664866L, 45935056L, 55449273L, 117769301L, 117892183L, 
167580307L, 172458743L, 172852403L, 129495492L, 129526092L, 198572L, 
112837740L, 112847206L, 132105712L, 132126171L, 150442L, 23261653L, 
23270278L, 28135989L), End = c(7539746L, 32664843L, 45934562L, 
55445562L, 117767918L, 117886211L, 167580003L, 172457651L, 172851634L, 
214938359L, 129525791L, 135506704L, 112837593L, 112846803L, 132104437L, 
132122974L, 134944770L, 23261612L, 23268885L, 28131521L, 52920414L
)), .Names = c("sampleID", "Chromosome", "Start", "End"), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1000L, 1001L, 1002L, 1003L, 
1004L, 1005L, 1006L, 1007L, 1008L, 1009L, 1010L), class = "data.frame")