Python 3.x 如何让用户选择python打印多少行?
我正在做家庭作业,我需要这样做,以便用户可以选择打印的行数。这是我第一次发布在这里,如果我做了一些不正确的事情,请让我知道。谢谢Python 3.x 如何让用户选择python打印多少行?,python-3.x,Python 3.x,我正在做家庭作业,我需要这样做,以便用户可以选择打印的行数。这是我第一次发布在这里,如果我做了一些不正确的事情,请让我知道。谢谢 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!")
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()
我会按照以下思路做一些事情:
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()
我需要它,这样它就可以键入用户想要的行数。所以,如果用户输入1,完整的程序是25行,我需要输出是老麦克唐纳有一个农场,Ee-igh,Ee-igh,哦!停在这一行,好吧,我做了改变。