Pages Navigation Menu

"It's not about the destination, it's about the journey. "

Learn Python The Hard Way, Exercise 19

I’ve now moved onto exercise 19.

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)

Explaining each line

Line 1

On line 1 we have the start of the mini-script (“def”) and then we have cheese_and_crackers which is the function that will be used throughout the rest of the script. For this first function/mini-script we have “cheese_count” and “boxes_of_crackers”) inside the parenthesis. We’ll use these on lines 2 and 3.

Lines 2 – 8

On line 2 we have %d that uses “% cheese_count” and that “name” is on line 1; the same goes with line 2. The other lines are too basic to explain, so I’m just going to skip to line 8, which is where the data is pulled when executing/running the file, so that line 8 “cheese_and_crackers(20, 30)” with the numbers corresponding and displaying when the file is executed.

Lines 10 – 14

The more direct way of imputing numbers is using variables and in this case we declare the numbers after creating a variable name then after the equation mark we enter the value (Lines 11 – 12).  So we can re-use the function data from “cheese_and_crackers” we use it when  calling for the variables so it re-uses the data in lines 1 – 3; however, uses the values within the variables and not the values in line 8.

After explaining all those lines, the rest of the lines make sense now and I feel I have a clear grasp of it now.

Output in Windows Powershell:

We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.

OR, we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.

We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.

And we can combine the two, variables and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.

Next step

So, after learning and studying this, I moved onto the next step. The next step was creating something of my own and seeing if it would work. Since I like the raw_input() function, as you can add user input functionality, I tried and succeeded in adapting raw_input() with a def function.

Here’s what I got:

def hello_world(name, country, favcountry, favgame, languages):
	print "\n"
	print "This is how you responded to those questions:\n"
	print "Your first name is: %r" % (name) 
	print "Your country of origin is: %r" % (country)
	print "Your favourite country is: %r" % (favcountry)
	print "Your favourite game is: %r" % (favgame)
	print "You said you can speak these languages: %r" % (languages) 
	print "That's all! \n"

print "Please answer the following questions." 
hello_world(raw_input("First name: "), 
raw_input("Your country of origin: "), 
raw_input("What's favourite country?: "), 
raw_input("What's favourite game?: "), 
raw_input("What languages can you speak? (Separate with a comma): ")

It works and it isn’t too shabby. However, according to this exercise I have to use 10 other functions in the same file to show that I really know how to use this newly learnt method and probably to practice other functions other than those I’ve been using for a long time! I’ve only used one type of function which is raw_input(); however, I’m a little tired of this exercise so I’m going to move onto exercise 20 and I’ll return to this exercise, if need be it!



    Good resource for this particular course.

    • Tis’ an old resource, but looks good nonetheless.

Leave a Comment

Your email address will not be published. Required fields are marked *