在Python中定义多个函数

时间:2014-03-06 08:43:18

标签: python

我是编写代码的新手,我正在努力学习“以艰难的方式学习Python”。我想定义PersonName和Hoarder,以便得到以下结果:

弗雷德有45只猫 比尔有20只狗 那是很多动物 找一位治疗师!

def PersonName(name1, name2): 
def Hoarder(number_of_cats, number_of_dogs):
    print "%r had %d cats." % (name1, number_of_cats)
    print "%r had %d dogs." % (name2, number_of_dogs)
    print "That is a lot of animals."
    print "Find a therapist!"

print
Person("Fred", "Bill")
Hoarder(45,20)

1 个答案:

答案 0 :(得分:0)

def PersonName(name1, name2): 
    def Hoarder(number_of_cats, number_of_dogs):
        print "%r had %d cats." % (name1, number_of_cats)
        print "%r had %d dogs." % (name2, number_of_dogs)
        print "That is a lot of animals."
        print "Find a therapist!"
    Hoarder(45,20)

PersonName("Fred", "Bill")

'Fred' had 45 cats.
'Bill' had 20 dogs.
That is a lot of animals.
Find a therapist!

如果你真的想要定义两个函数,你需要像上面那样编写它们。另请注意功能范围。