Learn Python the Hard way
In [2]:
#exeercise 18
def print_two(*args):
arg1, arg2 = args
print ("arg1: ", arg1, "arg2: ", arg2)
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
print ("arg1: ", arg1, "arg2: ", arg2)
# this just takes one argument
def print_one(arg1):
print ("arg1: ", arg1)
# this one takes no arguments
def print_none():
print ("I got nothin'.")
print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
In [3]:
#exercise 19
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print ("You have ",cheese_count, "cheeses!")
print ("You have ", 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)
In [14]:
#exercise 20
input_file= "junk.txt"
#reading a file
def to_print_a_file(f):
print (f.read())
def rewind(f):
f.seek(0)
def print_a_line(line_count,f):
print(line_count, f.readline())
current_file= open(input_file)
print ("First let's print the whole file:\n")
to_print_a_file(current_file)
print("Now let's rewind, kind of like a tape.")
rewind(current_file)
print ("Let's print three lines:\n")
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
In [18]:
#exercise 21
def add(a,b):
print("ADDING",a,b)
return a+b
def subtract(a, b):
print ("Subtracting ", a, b)
return a - b
def multiply(a, b):
print ("Multiplying ", a, b)
return a * b
def divide(a, b):
print ("Dividing ", a, b)
return a / b
print("Let's do some math with just functions")
age=add(30,5)
height= subtract(78,4)
weight=multiply(90,2)
q=divide(100,2)
print ("Age: Height: Weight: IQ: " , age, height, weight, q)
#a puzzle for extra credit,type it anyway
print(" here is a puzzle")
what=add(age,subtract(height,multiply(weight, divide(q, 2))))
print ("That becomes: ", what, "Can you do it by hand?")
In [31]:
#exercise 24
print ("Let's practice everything.")
print ('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')
poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print("......")
print(poem)
print("......")
five= 10-2+3-6
print("This should be five:", five)
def secret_formula(started):
jelly_beans=started*500
jars=jelly_beans/1000
crates=jars/100
return jelly_beans,jars,crates
beans,jars,crates=secret_formula(10000)
print ("With a starting point of: ",10000)
print ("We'd have ",beans," beans,", jars, " jars and ", crates, " crates.")
print("We can also do this way")
print("We have %d beans, %d jars, %d crates." % secret_formula(1000))
In [32]:
#exercise 29
people = 20
cats = 30
dogs = 15
if people < cats:
print ("Too many cats! The world is doomed!")
if people > cats:
print ("Not many cats! The world is saved!")
if people < dogs:
print ("The world is drooled on!")
if people > dogs:
print ("The world is dry!")
dogs+=5
if people >= dogs:
print ("People are greater than or equal to dogs.")
if people <= dogs:
print ("People are less than or equal to dogs.")
if people == dogs:
print ("People are dogs.")
In [36]:
#exercise 30
people=30
cars=40
trucks=15
if cars > people:
print ("We should take the cars.")
elif cars < people:
print ("We should not take the cars.")
else:
print ("We can't decide.")
if trucks > cars:
print ("That's too many trucks.")
elif trucks < cars:
print ("Maybe we could take the trucks.")
else:
print ("We still can't decide.")
if people > trucks:
print ("Alright, let's just take the trucks.")
else:
print ("Fine, let's stay home then.")
Comments
Post a Comment