This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("> ") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # LPTHW Exercise 29 | |
| people = 20 | |
| cats = 30 | |
| dogs = 15 | |
| if people < cats: | |
| print "Too many cats! The world is doomed!" | |
| if people > cats: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # LPTHW - Exercise 20 | |
| from sys import argv | |
| script, input_file = argv | |
| def print_all(f): | |
| print f.read() | |
| def rewind(f): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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? |