Learn Python The Hard Way, Exercise 19

by | Mar 14, 2013 | Updates

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!

January 2020 – Running & Fitness

January 2020 – Running & Fitness

Originally created on January 1st, 2020: Updated daily until the end of January. I've decided that I will be keeping a log this year of all the runs that I've done this year by day and have a blog page for each month of the year. This blog post specifically will be...

Saint Espresso & Kitchen, Kentish Town

Saint Espresso & Kitchen, Kentish Town

Hanging around or about in Kentish Town? Want to go somewhere nice for brunch? Why not stop by over at Saint Espresso kitchen, just a 1 minute walk from Kentish Town underground station. The cafe is usually quite busy on the weekends and at around lunch time, so...

Hot Wok Kitchen, Archway

Hot Wok Kitchen, Archway

If you are looking for a peng place to eat, and that's close to Archway or Upper Holloway, then look no further than the restaurant that is Hot Wok Kitchen.It is a Japanese restaurant that serves excellent food and in fact, I'd say they are the best Japanese...

London Marathon 2015 – Training & Thoughts – 42.1km

London Marathon 2015 – Training & Thoughts – 42.1km

This is more of a retrospective post, looking at how I managed to do the London Marathon on April 26th, 2015. The London Marathon was my first, and currently only marathon that I've ever done. It holds a special milestone in my life, given all the issues I had gone...

What weight is a good weight to get back to?

What weight is a good weight to get back to?

As part of my plan/goal for 2020, I want to lose weight and get jacked up. I've gone through quite a journey, historically speaking, and given my sort of bumpy road of losing weight, then gaining weight, and then being content, and then not being content with my...

Becoming a Taiwanese national through descent…

Becoming a Taiwanese national through descent…

Originally posted Sep 6, 2011. Updated: Jan 3, 2020. Once Taiwanese you will be required to serve 12 months as a conscript in the Taiwanese military. Anyways, this blog post is essentially just the process you'll go through to get all my documents verified by two...

First run of 2020 – 5.89km

First run of 2020 – 5.89km

I have not run properly in a good while so I found today's run quite tough actually. It was definitely not an easy run - given that Christmas and New Year's had me eat a lot of food and frankly, I've not been eating with health in mind - let's just say. So as part of...

Happy New Year #2020 from London

Happy New Year #2020 from London

It's now officially 13 minutes past, and we're well into 2020. For the past 3 years, I've been going to Hampstead Heath Park with Victoria Levine, to celebrate with the many hundreds that turn up to watch the London firework celebrations from afar: Here you can see a...