I am doing a homework assignment and I need to make it so the user can choose the amount of lines is printed. This is my first time posted here so if I'm doing something incorrect please let me know. Thanks

def verse():
    print ("Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!")


def sing(animal, sound):
    verse()
    print ("And on the farm he had a ", animal + ", Ee-igh, Ee-igh, Oh!")
    print ("With a", sound + ",", sound, "here and a" , sound + ",",sound + " there.")
    print ("Here a", sound + ", there a", sound + ", everyehere a",sound  + ",", sound + ".")
    verse()


def main():
    lines = input("Enter the amount of lines you would like to see:")
    #not sure what to put next
    sing("cow", "moo")
    print()
    sing("horse", "neigh")
    print()
    sing("pig", "oink")
    print()
    sing("goat","baa")
    print()
    sing("hen", "cluck")

main()

没有正确的解决方案

其他提示

I would do something along these lines:

def sing(lines):
    template='''
    Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!
    And on the farm he had a {animal}
    Ee-igh, Ee-igh, Oh!
    With a {sound}, {sound} here and a {sound}, {sound} there.
    Here a, {sound}, there a, {sound}, everywhere a, {sound}, {sound}.
    Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!'''

    verses=[("cow", "moo"),
            ("horse", "neigh"),
            ("pig", "oink"),
            ("goat","baa"),
            ("hen", "cluck")]
    li=[]        
    for n in range(0,5):        
        li.append(template.format(animal=verses[n][0], sound=verses[n][1]))   

    song=''.join(li)
    return '\n'.join(song.splitlines()[0:lines+1])         

def main():
    lines = input("Enter the amount of lines you would like to see: ")
    print(sing(int(lines)))

main()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top