Lesson 19

Hoo-whee, we are really getting into it now, aren’t we! I know this blog is only a week old, but I’ve been working on Learn Python the Hard Way for just shy of two months, and that I’ve gotten through 19 lessons is quite exciting for me! Getting somewhere, even if I am not being assessed – now THERE’S adulthood! Well, maybe it’d be more adult if it wasn’t just so fun!

This exercise deals more with the introduction to writing and using one’s own functions, just like last time.

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

We start out by defining cheese_and_crackers, a function that prints out a few statements with the given variables. Just a skosh down the road, we start putting values in for cheese_count and boxes_of_crackers, but suddenly! ack!

amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)

Now we’re calling cheese_count by the name of amount_of_cheese! WELL. The neat thing here is that in defining the function, we’ve named it something convenient for ourselves, but when we refer to it later, the var should be called something else, to distinguish from the name/s given in the initially defined function. Follow me here:

def function(thing_a, thing_b)
print "here's the %s things and the %s other things" % (thing_a, thing_b)

number_of_things_boop = 20
number_of_things_doop = 30
function(number_of_things_boop, number_of_things_doop)

So that last bit calls the function itself, to which the variables .._boop and .._doop are passed, and since the function initially requires two variables, any two passed in there will then be given the roles of thing_a and thing_b given in the function. Hoo! I have read this paragraph aloud to myself several times, and I think that it makes sense?

So that’s Lesson 19, and it’s also the lesson in which I learned about the html code tag! The explanation is much lengthier than my understanding would have given, but I’m really pleased to have ironed it out so thoroughly, at least for myself. Crystal clear, now!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s