我正在做Zed Shaw的LPTHW的书,但我被困在这额外的信贷3 question,发现这里没有任何问题,因此我决定做一个帐户。LPTHW,EX19额外信贷3
额外功劳分配: 至少写一个自己设计的功能,并运行它10种不同的方式。
代码:
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print "You have %d cheeses!" % cheese_count
print "You have %d boxes of crackers!" % boxes_of_crackers
print "Man that's enough for a party!"
print "Get a blanket.\n"
print "We can just give the function numbers directly:"
cheese_and_crackers(20, 30)
print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)
print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
那么,有什么其他的方式来运行你的脚本里面的函数吗?您能否以初学者的详细方式帮助我,包括实际的代码,以便我可以尝试并理解它?
您可以随时将函数分配给一个变量,然后以这种方式调用它。例如。 “foo = cheese_and_crackers; foo(10,20)'''''''''''''确实是一种微不足道的东西 – 2012-02-12 23:29:02
如何在一个循环中运行它?或者将该函数传递给另一个函数,它会调用它? – 2012-02-13 00:05:25
@PeterRowell嗯,我是一个初学者,我只是想找出更多的方法来运行函数,感谢您的回复,我不知道我可以将函数赋值给变量,如果您知道其他方式,而不管它们的“编程美学“请写下来,谢谢! – Drageek 2012-02-13 00:17:38