我正在做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