[Tutor] Learn Python The Hard Way, Ex19-3
Dave Angel
d at davea.name
Sat Feb 11 23:47:20 CET 2012
On 02/11/2012 05:26 PM, amt wrote:
> Hello! I'm currently stuck at the Extra Credit 3 from LPTHW.
>
> Link to the actual exercise:http://learnpythonthehardway.org/book/ex19.html
> The exercise:
> Write at least one more function of your own design, and run it 10
> different ways.
>
>
> Code from the book:
> 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)
>
>
>
>
>
> I wrote a function similar to cheese_and_crackers and it works just
> fine but I can't figure out more ways of calling a function other than
> the ones presented in the code(with integers as arguments,variables as
> arguments, two integer additions as arguments and with arguments in
> the form of variable+integer). The author states that there are 10
> different ways to run it.(in a comment he states that: "You can run it
> a lot of different ways, far too many to enumerate.).
>
>
> So, what other ways are there aside the ones already presented in the
> above code?
>
I don't have time to get the book and figure out how much of Python
you've covered, but let me give it a shot:
food = (3, 9)
cheese_and_crackers(*food)
Since they say you can use a function of your own design, how about
giving it default values for one or more arguments?
if the function is
def cheese_and_crackers(cheese_count, boxes_of_crackers=1):
You can now call it as cheese_and_crackers(5)
You can call it using named arguments:
cheese_and_crackers(boxes_of_crackers=5, cheese_count=12)
If you're allowed to use the library,
import functools
aaa = functools.partial(cheese_and_crackers, 3)
then call aaa(19)
That's all I've got right now, but we could really go crazy...
--
DaveA
More information about the Tutor
mailing list