Pages Navigation Menu

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

Learn Python The Hard Way, Exercise 19

Posted by on Mar 14, 2013 in Programming

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”)...

Read More

Learn Python The Hard Way, exercise 18

Posted by on Mar 14, 2013 in Programming

I found this exercise to be quite comforting; however, at first I was a little confused when presented with all that new code that I had never seen before. In this exercise it teaches you about the “def” (Define) and creating functions (Mini-scripts). To overcome my confusion, I typed the whole thing out and ran it in Windows Powershell. Then for extra practice I created another file and typed each function one by one and changed those functions to get an idea on what each and every thing does. Here’s my code: # FUNCTION 1 def print_two(*args): arg1, arg2, arg3 = args print "arg1: %r, arg2: %r, arg3: %r" % (arg1, arg2, arg3) print_two("Hello","Mr","Python") # FUNCTION 2 def print_two_again(arg1, arg2): print "arg1: %r, arg2: %r" % (arg1, arg2) print_two_again("Hello2","World!") # FUNCTION 3 def print_none(): print "Nothing here." print_none() I only modified...

Read More

Learn Python The Hard Way, Exercise 17

Posted by on Mar 14, 2013 in Programming

I’m currently working/studying exercise 17 from Learn Python the Hard Way. In this exercise I learned two new things, and those two reasons are why I created this new category which will cater for my programming experiences, as opposed to me creating hundreds of pages and having my navigation bar up-top filled with drop down menus on the “code” navigation link. As for my programming abilities so far, I’ve been typing out every single piece of code in this eBook by hand (As requested by the author) with no copy and pasting whatsoever! I’m quite proud of that actually. Anyways, in the exercise it states that I need to condense the following code: from sys import argv from os.path import exists script, from_file, to_file = argv print "Copying from %s to %s" % (from_file, to_file) # we could do these two...

Read More