Skip to content

Instantly share code, notes, and snippets.

@adaptives
adaptives / ex31.py
Created September 27, 2011 09:14
LPTHW Exercise 31
print "You enter a dark room with two doors. Do you go through door #1 or door #2?"
door = raw_input("> ")
if door == "1":
print "There's a giant bear here eating a cheese cake. What do you do?"
print "1. Take the cake."
print "2. Scream at the bear."
bear = raw_input("> ")
@adaptives
adaptives / ex30.py
Created September 27, 2011 09:05
LPTHW Exercise 30
#LPTHW Exercise 30
people = 30
cars = 40
buses = 15
if cars > people:
print "We should take the cars."
elif cars < people:
print "We should not take the cars."
@adaptives
adaptives / ex29.py
Created September 27, 2011 08:57
LPTHW Exercise 29
# LPTHW Exercise 29
people = 20
cats = 30
dogs = 15
if people < cats:
print "Too many cats! The world is doomed!"
if people > cats:
@adaptives
adaptives / ex25.py
Created September 27, 2011 07:31
LPTHW Exercise 25
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
@adaptives
adaptives / ex24.py
Created September 27, 2011 07:01
Exercise 24
#LPTHW 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 compressed passion from intuition
@adaptives
adaptives / ex21.py
Created September 23, 2011 09:41
LPYHW 2.0 - Exercise 21
def add(a, b):
print "ADDING %d + %d" % (a,b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
@adaptives
adaptives / ex20.py
Created September 14, 2011 08:02
LPTHW - Exercise 20
# LPTHW - Exercise 20
from sys import argv
script, input_file = argv
def print_all(f):
print f.read()
def rewind(f):
@adaptives
adaptives / ex19.py
Created September 14, 2011 07:52
LPTHW - Exercise 19
#LPTHW - 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)
@adaptives
adaptives / ex18.py
Created September 14, 2011 07:41
LPTHW - Exercise 18
# LPTHW - Exercise 18
# this one islike your scripts with argv
def print_two(*args):
arg1, arg2 = args
print "arg1: %r, arg2: %r" % (arg1, arg2)
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
print "arg1: %r, arg2: %r" % (arg1, arg2)
@adaptives
adaptives / gist:1216019
Created September 14, 2011 07:10
LPTHW - Exercise 17 (Copying files)
# LPTHW - Exercise 17
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 on one line too, how?