├── .gitignore ├── README ├── ex1.py ├── ex10.py ├── ex11.py ├── ex12.py ├── ex13-less.py ├── ex13-more.py ├── ex13-raw_input.py ├── ex13.py ├── ex14.py ├── ex15.py ├── ex15_sample.txt ├── ex16-read.py ├── ex16.py ├── ex17.py ├── ex18.py ├── ex19-mine.py ├── ex19.py ├── ex2.py ├── ex20.py ├── ex21.py ├── ex24.py ├── ex25.py ├── ex26.py ├── ex28.py ├── ex29.py ├── ex3-floatingpoint.py ├── ex3.py ├── ex30.py ├── ex31.py ├── ex32.py ├── ex33.py ├── ex35.py ├── ex4.py ├── ex5.py ├── ex6.py ├── ex7.py ├── ex8.py └── ex9.py /.gitignore: -------------------------------------------------------------------------------- 1 | copied.txt 2 | test.txt 3 | test2.txt -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Just going through "Learn Python The Hard Way." 2 | 3 | Follow along if you want! -------------------------------------------------------------------------------- /ex1.py: -------------------------------------------------------------------------------- 1 | print "Hello World!" 2 | print "Hello Again" 3 | print "I like typing this." 4 | print "This is fun." 5 | print 'Yay! Printing.' 6 | print "I'd much rather you 'not'." 7 | # print 'I "said" do not touch this.' 8 | print 'This is another line!' -------------------------------------------------------------------------------- /ex10.py: -------------------------------------------------------------------------------- 1 | tabby_cat = "\tI'm tabbed in." 2 | persian_cat = "I'm split\non a line." 3 | backslash_cat = "I'm \\ a \\ cat." 4 | 5 | fat_cat = """ 6 | I'll do a list: 7 | \t* Cat food 8 | \t* Fishies 9 | \t* Catnip\n\t* Grass 10 | """ 11 | 12 | print tabby_cat 13 | print persian_cat 14 | print backslash_cat 15 | print fat_cat 16 | 17 | # 1. All python escape sequences: \\, \', \", \a, \b, \f, \n, \N{name}, \r, \t, \uxxxx, \Uxxxxxxxx, \v, \ooo, \xhh 18 | # 2. If you were printing python code, you'd need to use ''' if you were printing a long python string starting with """ -------------------------------------------------------------------------------- /ex11.py: -------------------------------------------------------------------------------- 1 | print "What's your name?" 2 | name = raw_input('==>') 3 | print "How old are you?", 4 | age = raw_input() 5 | print "How many tall are you?", 6 | height = raw_input() 7 | print "How much do you weigh?", 8 | weight = raw_input() 9 | 10 | print "So, your name is %r, you're %r old, %r tall and %r heavy." % ( 11 | name, age, height, weight) 12 | 13 | # 1. raw_input() takes input from the user, and always returns a string -------------------------------------------------------------------------------- /ex12.py: -------------------------------------------------------------------------------- 1 | age = raw_input("How old are you? ") 2 | height = raw_input("How many tall are you? ") 3 | weight = raw_input("How much do you weigh? ") 4 | 5 | print "So, you're %r old, %r tall and %r heavy." % ( 6 | age, height, weight) -------------------------------------------------------------------------------- /ex13-less.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, first_name, last_name = argv 4 | 5 | print "Your full name is %s %s" % (first_name, last_name) -------------------------------------------------------------------------------- /ex13-more.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, num1, num2, num3, num4 = argv 4 | 5 | print "The four numbers you entered are: %s, %s, %s and %s" % (num1, num2, num3, num4) -------------------------------------------------------------------------------- /ex13-raw_input.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | try: # https://docs.python.org/3/whatsnew/3.0.html#builtins 4 | raw_input # Python 2 5 | except NameError: 6 | raw_input = input # Python 3 7 | 8 | script, first_name, last_name = argv 9 | 10 | middle_name = raw_input("What is your middle name? ") 11 | 12 | print "Your full name is %s %s %s." % (first_name, middle_name, last_name) 13 | -------------------------------------------------------------------------------- /ex13.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, first, second, third = argv 4 | 5 | print "The script is called:", script 6 | print "The first variable is:", first 7 | print "The second variable is:", second 8 | print "Your third variable is:", third 9 | 10 | # Extra credit 11 | # 1. Error: ValueError: need more than 2 values to unpack 12 | # This means that the script needs the correct amount of arguments (3) to process correctly -------------------------------------------------------------------------------- /ex14.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, user_name, fave_color = argv 4 | prompt = '8====D~~~ ' 5 | 6 | print "Hi %s, I'm the %s script." % (user_name, script) 7 | print "I'd like to ask you a few questions." 8 | print "Do you like me %s?" % user_name 9 | likes = raw_input(prompt) 10 | 11 | print "Where do you live %s?" % user_name 12 | lives = raw_input(prompt) 13 | 14 | print "What kind of computer do you have?" 15 | computer = raw_input(prompt) 16 | 17 | print """ 18 | Alright, so you said %r about liking me. 19 | You live in %r. Not sure where that is. 20 | And you have a %r computer. Nice. 21 | Your favorite color is %r. Mine too! 22 | """ % (likes, lives, computer, fave_color) -------------------------------------------------------------------------------- /ex15.py: -------------------------------------------------------------------------------- 1 | # import argv from the sys module 2 | from sys import argv 3 | 4 | # set script and filename variables from the script arguments 5 | script, filename = argv 6 | 7 | # open the filename passed in as an argument, and assign the file object to the txt variable 8 | txt = open(filename) 9 | 10 | # print a string with the filename 11 | print "Here's your file %r:" % filename 12 | 13 | # print the contexts of the txt file object 14 | print txt.read() 15 | 16 | txt.close() 17 | 18 | # print a string 19 | print "I'll also ask you to type it again:" 20 | 21 | # prompt the user for a file name 22 | file_again = raw_input("> ") 23 | 24 | # open the file that the user entered, and then assign the file object to the variable txt_again 25 | txt_again = open(file_again) 26 | 27 | # print the contents of the txt_again file object 28 | print txt_again.readlines() 29 | 30 | txt_again.close() -------------------------------------------------------------------------------- /ex15_sample.txt: -------------------------------------------------------------------------------- 1 | This is stuff I typed into a file. 2 | It is really cool stuff. 3 | Lots and lots of fun to have in here. -------------------------------------------------------------------------------- /ex16-read.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, filename = argv 4 | 5 | print "Opening %r..." % filename 6 | f = open(filename) 7 | 8 | print "Printing the contents of %r..." % filename 9 | print f.read() -------------------------------------------------------------------------------- /ex16.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, filename = argv 4 | 5 | print "We're going to erase %r." % filename 6 | print "If you don't want that, hit CTRL-C (^C)." 7 | print "If you do want that, hit RETURN." 8 | 9 | raw_input("?") 10 | 11 | print "Opening the file..." 12 | target = open(filename, 'w') 13 | 14 | print "Truncating the file. Goodbye!" 15 | target.truncate() 16 | 17 | print "Now I'm going to ask you for three lines." 18 | 19 | line1 = raw_input("line 1: ") 20 | line2 = raw_input("line 2: ") 21 | line3 = raw_input("line 3: ") 22 | 23 | print "I'm going to write these to the file." 24 | 25 | target.write("%s\n%s\n%s\n" % (line1, line2, line3)) 26 | 27 | print "And finally, we close it." 28 | target.close() -------------------------------------------------------------------------------- /ex17.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | from os.path import exists 3 | 4 | script, from_file, to_file = argv 5 | 6 | open(to_file, 'w').write(open(from_file).read()) -------------------------------------------------------------------------------- /ex18.py: -------------------------------------------------------------------------------- 1 | # this one is like your scripts with argv 2 | def print_two(*args): 3 | arg1, arg2 = args 4 | print "arg1: %r, arg2: %r" % (arg1, arg2) 5 | 6 | # ok, that *args is actually pointless, we can just do this 7 | def print_two_again(arg1, arg2): 8 | print "arg1: %r, arg2: %r" % (arg1, arg2) 9 | 10 | # this just takes one argument 11 | def print_one(arg1): 12 | print "arg1: %r" % arg1 13 | 14 | # this one takes no arguments 15 | def print_none(): 16 | print "I got nothin'." 17 | 18 | print_two("Zed","Shaw") 19 | print_two_again("Zed","Shaw") 20 | print_one("First!") 21 | print_none() -------------------------------------------------------------------------------- /ex19-mine.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, filename = argv 4 | 5 | def read_file(filename): 6 | f = open(filename) 7 | print f.read() 8 | 9 | read_file(filename) 10 | read_file(raw_input("Enter a filename: ")) 11 | read_file("copied.txt") 12 | 13 | filename = "ex1.py" 14 | read_file(filename) -------------------------------------------------------------------------------- /ex19.py: -------------------------------------------------------------------------------- 1 | # define the chees_and_crackers function 2 | def cheese_and_crackers(cheese_count, boxes_of_crackers): 3 | # print the number of cheeses 4 | print "You have %d cheeeses!" % cheese_count 5 | # print the number of boxes of crackers 6 | print "You have %d boxes of crackers!" % boxes_of_crackers 7 | # print a string 8 | print "Man that's enough for a party!" 9 | # print a string with a line break 10 | print "Get a blanket.\n" 11 | 12 | # print a string 13 | print "We can just give the function numbers directly:" 14 | # pass 20, 30 to the cheese_and_crackers function 15 | cheese_and_crackers(20, 30) 16 | 17 | # print a string 18 | print "OR, we can use variables from our script:" 19 | # set a variable to 10 20 | amount_of_cheese = 10 21 | # set a variable to 50 22 | amount_of_crackers = 50 23 | 24 | # pass amount_of_cheese and amount_of_crackers variables to cheese_and_crackers function 25 | cheese_and_crackers(amount_of_cheese, amount_of_crackers) 26 | 27 | # print a string 28 | print "We can even to math inside too:" 29 | # pass 10 + 20 and 5 + 6 to the cheese_and_crackers function 30 | cheese_and_crackers(10 + 20, 5 + 6) 31 | 32 | # print a string 33 | print "And we can combine the two, variables and math:" 34 | # pass the amount of cheese variable + 100 and the amount_of_crackers variable + 1000 to the cheese_and_crackers function 35 | cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) -------------------------------------------------------------------------------- /ex2.py: -------------------------------------------------------------------------------- 1 | # A comment, this is so you can read your program later. 2 | # Anything after the # is ignored by python. 3 | 4 | print "I could have code like this." # and the comment after is ignored 5 | 6 | # You can also use a comment to "disable" or comment out a piece of code: 7 | # print "This won't run." 8 | 9 | print "This will run." -------------------------------------------------------------------------------- /ex20.py: -------------------------------------------------------------------------------- 1 | # import argv from sys 2 | from sys import argv 3 | 4 | # get arguments 5 | script, input_file = argv 6 | 7 | # define print_all function, which takes 1 argument (f) 8 | def print_all(f): 9 | # print the contents of f 10 | print f.read() 11 | 12 | # define the rewind function, which takes 1 argument (f) 13 | def rewind(f): 14 | # move the file pointer back to the beginning of file f 15 | f.seek(0) 16 | 17 | # define the print_a_line function, which takes 2 arguments (line_count, f) 18 | def print_a_line(line_count, f): 19 | # print line_count, then a line from file f 20 | print line_count, f.readline() 21 | 22 | # set current_file variable to a file pointer to input_file 23 | current_file = open(input_file) 24 | 25 | # print string 26 | print "First let's print the whole file:\n" 27 | 28 | # call print_all function with current_file as an argument 29 | print_all(current_file) 30 | 31 | # print string 32 | print "Now let's rewind, kind of like a tape." 33 | 34 | # call rewind function with current_file as an argument 35 | rewind(current_file) 36 | 37 | # print string 38 | print "Let's print three lines:" 39 | 40 | # set current_line to 1 41 | current_line = 1 42 | # call print_a_line function with current_line and current_file as arguments 43 | print_a_line(current_line, current_file) 44 | 45 | # set current line to itself + 1 46 | current_line += 1 47 | # call print_a_line function with current_line and current_file as arguments 48 | print_a_line(current_line, current_file) 49 | 50 | # set current line to itself + 1 51 | current_line += 1 52 | # call print_a_line function with current_line and current_file as arguments 53 | print_a_line(current_line, current_file) -------------------------------------------------------------------------------- /ex21.py: -------------------------------------------------------------------------------- 1 | def add(a, b): 2 | print "ADDING %d + %d" % (a, b) 3 | return a + b 4 | 5 | def subtract(a, b): 6 | print "SUBTRACTING %d - %d" % (a, b) 7 | return a - b 8 | 9 | def multiply(a, b): 10 | print "MULTIPLYING %d * %d" % (a, b) 11 | return a * b 12 | 13 | def divide(a, b): 14 | print "DIVIDING %d / %d" % (a, b) 15 | return a / b 16 | 17 | 18 | print "Let's do some math with just functions!" 19 | 20 | age = add(30, 5) 21 | height = subtract(78, 4) 22 | weight = multiply(90, 2) 23 | iq = divide(100, 2) 24 | 25 | print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq) 26 | 27 | 28 | # A puzzle for the extra credit, type it in anyway 29 | print "Here is a puzzle." 30 | 31 | # age + height - weight * (iq / 2) 32 | # 35 + 74 - 180 * (50 / 2) 33 | what = add(age, subtract(height, multiply(weight, divide(iq, 4)))) 34 | 35 | print "That becomes: ", what, "Can you do it by hand?" 36 | 37 | # 25 + (75 + (125 * 3)) 38 | what2 = add(25, add(75, multiply(125, 3))) 39 | print "That becomes: ", what2 -------------------------------------------------------------------------------- /ex24.py: -------------------------------------------------------------------------------- 1 | print "Let's practice everything." 2 | print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.' 3 | 4 | poem = """ 5 | \tThe lovely world 6 | with logic so firmly planted 7 | cannot discern \n the needs of love 8 | nor comprehend passion from intuition 9 | and requires an explanation 10 | \n\t\twhere there is none. 11 | """ 12 | 13 | print "--------------" 14 | print poem 15 | print "--------------" 16 | 17 | 18 | five = 10 - 2 + 3 - 6 19 | print "This should be five: %s" % five 20 | 21 | def secret_formula(started): 22 | jelly_beans = started * 500 23 | jars = jelly_beans / 1000 24 | crates = jars / 100 25 | return jelly_beans, jars, crates 26 | 27 | 28 | start_point = 10000 29 | beans, jars, crates = secret_formula(start_point) 30 | 31 | print "With a starting point of: %d" % start_point 32 | print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates) 33 | 34 | start_point = start_point / 10 35 | 36 | print "We can also do that this way:" 37 | print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point) -------------------------------------------------------------------------------- /ex25.py: -------------------------------------------------------------------------------- 1 | def break_words(stuff): 2 | """This function will break up words for us.""" 3 | words = stuff.split(' ') 4 | return words 5 | 6 | def sort_words(words): 7 | """Sorts the words.""" 8 | return sorted(words) 9 | 10 | def print_first_word(words): 11 | """Prints the first word after popping it off.""" 12 | word = words.pop(0) 13 | print word 14 | 15 | def print_last_word(words): 16 | """Prints the last word after popping it off.""" 17 | word = words.pop(-1) 18 | print word 19 | 20 | def sort_sentence(sentence): 21 | """Takes in a full sentence and returns the sorted words.""" 22 | words = break_words(sentence) 23 | return sort_words(words) 24 | 25 | def print_first_and_last(sentence): 26 | """Prints the first and last words of the sentence.""" 27 | words = break_words(sentence) 28 | print_first_word(words) 29 | print_last_word(words) 30 | 31 | def print_first_and_last_sorted(sentence): 32 | """Sorts the words then prints the first and last one.""" 33 | words = sort_sentence(sentence) 34 | print_first_word(words) 35 | print_last_word(words) -------------------------------------------------------------------------------- /ex26.py: -------------------------------------------------------------------------------- 1 | def break_words(stuff): 2 | """This function will break up words for us.""" 3 | words = stuff.split(' ') 4 | return words 5 | 6 | def sort_words(words): 7 | """Sorts the words.""" 8 | return sorted(words) 9 | 10 | def print_first_word(words): 11 | """Prints the first word after popping it off.""" 12 | word = words.pop(0) 13 | print word 14 | 15 | def print_last_word(words): 16 | """Prints the last word after popping it off.""" 17 | word = words.pop(-1) 18 | print word 19 | 20 | def sort_sentence(sentence): 21 | """Takes in a full sentence and returns the sorted words.""" 22 | words = break_words(sentence) 23 | return sort_words(words) 24 | 25 | def print_first_and_last(sentence): 26 | """Prints the first and last words of the sentence.""" 27 | words = break_words(sentence) 28 | print_first_word(words) 29 | print_last_word(words) 30 | 31 | def print_first_and_last_sorted(sentence): 32 | """Sorts the words then prints the first and last one.""" 33 | words = sort_sentence(sentence) 34 | print_first_word(words) 35 | print_last_word(words) 36 | 37 | 38 | print "Let's practice everything." 39 | print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.' 40 | 41 | poem = """ 42 | \tThe lovely world 43 | with logic so firmly planted 44 | cannot discern \n the needs of love 45 | nor comprehend passion from intuition 46 | and requires an explanation 47 | \n\t\twhere there is none. 48 | """ 49 | 50 | 51 | print "--------------" 52 | print poem 53 | print "--------------" 54 | 55 | five = 10 - 2 + 3 - 6 56 | print "This should be five: %s" % five 57 | 58 | def secret_formula(started): 59 | jelly_beans = started * 500 60 | jars = jelly_beans / 1000 61 | crates = jars / 100 62 | return jelly_beans, jars, crates 63 | 64 | 65 | start_point = 10000 66 | beans, jars, crates = secret_formula(start_point) 67 | 68 | print "With a starting point of: %d" % start_point 69 | print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates) 70 | 71 | start_point = start_point / 10 72 | 73 | print "We can also do that this way:" 74 | print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point) 75 | 76 | 77 | sentence = "All good things come to those who wait." 78 | 79 | words = break_words(sentence) 80 | sorted_words = sort_words(words) 81 | 82 | print_first_word(words) 83 | print_last_word(words) 84 | print_first_word(sorted_words) 85 | print_last_word(sorted_words) 86 | sorted_words = sort_sentence(sentence) 87 | print sorted_words 88 | 89 | print_first_and_last(sentence) 90 | 91 | print_first_and_last_sorted(sentence) -------------------------------------------------------------------------------- /ex28.py: -------------------------------------------------------------------------------- 1 | # 1. True 2 | # 2. False 3 | # 3. False 4 | # 4. True 5 | # 5. True 6 | # 6. True 7 | # 7. False 8 | # 8. True 9 | # 9. False 10 | #10. False 11 | #11. True 12 | #12. False 13 | #13. True 14 | #14. False 15 | #15. False 16 | #16. False 17 | #17. True 18 | #18. True 19 | #19. False 20 | #20. False 21 | 22 | # List of equality operators: <, >, ==, !=, <=, >=, is, is not 23 | # less than, greater than, equal to, not equal to, less than or equal to, greater than or equal to, is, is not -------------------------------------------------------------------------------- /ex29.py: -------------------------------------------------------------------------------- 1 | people = 20 2 | cats = 15 3 | dogs = 30 4 | 5 | 6 | if people < cats: 7 | print "Too many cats! The world is doomed!" 8 | 9 | if people > cats: 10 | print "Not many cats! The world is saved!" 11 | 12 | if people < dogs: 13 | print "The world is drooled on!" 14 | 15 | if people > dogs: 16 | print "The world is dry!" 17 | 18 | 19 | dogs += 5 20 | 21 | if people >= dogs: 22 | print "People are greater than equal to dogs." 23 | 24 | if people <= dogs: 25 | print "People are less than equal to dogs." 26 | 27 | 28 | if people == dogs: 29 | print "People are dogs." 30 | 31 | # Extra credit 32 | # 1. The if statement executes the code under it if the expression is True 33 | # 2. The code under the if needs to be indented 4 spaces so that Python knows what code is part of the if block 34 | # 3. If the code wasn't indented, it would be executed, regardless of the Truthiness of the if statement 35 | # 5. IF we change the initial variables, we get different print statements. -------------------------------------------------------------------------------- /ex3-floatingpoint.py: -------------------------------------------------------------------------------- 1 | print "I will now count my chickens:" 2 | 3 | print "Hens", 25.0 + 30 / 6 4 | 5 | print "Roosters", 100.0 - 25 * 3 % 4 6 | 7 | print "Now I will count the eggs:" 8 | print 3.0 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 9 | 10 | print "Is it true that 3 + 2 < 5 - 7?" 11 | print 3.0 + 2 < 5.0 - 7 12 | 13 | print "What is 3 + 2?", 3.0 + 2 14 | 15 | print "What is 5 - 7", 5.0 - 7 16 | 17 | print "Oh, that's why it's False." 18 | 19 | print "How about some more." 20 | 21 | print "Is it greater?", 5.0 > -2 22 | 23 | print "Is it greater or equal?", 5 >= -2 24 | 25 | print "Is it less or equal?", 5 <= -2 -------------------------------------------------------------------------------- /ex3.py: -------------------------------------------------------------------------------- 1 | print "I will now count my chickens:" 2 | 3 | # add 25 to (30 divided by 6) 4 | print "Hens", 25 + 30 / 6 5 | 6 | # subtract 100 by ((25 times 3) mod 4) 7 | print "Roosters", 100 - 25 * 3 % 4 8 | 9 | print "Now I will count the eggs:" 10 | 11 | # 3 plus 2 plus 1 minus 5 plus (4 mod 2) minus (1 divided by 4) plus six 12 | # 0 0 13 | print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 14 | 15 | print "Is it true that 3 + 2 < 5 - 7?" 16 | 17 | # test if 5 < -2 18 | print 3 + 2 < 5 - 7 19 | 20 | # add 3 plus 2 21 | print "What is 3 + 2?", 3 + 2 22 | 23 | # subtract 7 from 5 24 | print "What is 5 - 7?", 5 - 7 25 | 26 | print "Oh, that's why it's False." 27 | 28 | print "How about some more." 29 | 30 | # test if 5 is greater than -2 31 | print "Is it greater?", 5 > -2 32 | 33 | # test if 5 is greater or equay to -2 34 | print "Is it greater or equal?", 5 >= -2 35 | 36 | # test if 5 is less than or equal to -2 37 | print "Is it less or equal?", 5 <= -2 -------------------------------------------------------------------------------- /ex30.py: -------------------------------------------------------------------------------- 1 | # sets the variable "people" to 50 2 | people = 50 3 | 4 | # sets the variable "cars" to 60 5 | cars = 60 6 | 7 | # sets the variables "buses" to 50 8 | buses = 50 9 | 10 | # if "cars is greater than people" 11 | if cars > people: 12 | # print string 13 | print "We should take the cars." 14 | 15 | # else if "cars is less than people" 16 | elif cars < people: 17 | # print string 18 | print "We should not take the cars." 19 | 20 | # else 21 | else: 22 | # print string 23 | print "We can't decide." 24 | 25 | # if "buses greater than cars" 26 | if buses > cars: 27 | # print string 28 | print "That's too many buses." 29 | 30 | # else if "buses less than cars" 31 | elif buses < cars: 32 | # print string 33 | print "Maybe we could take the buses." 34 | # else 35 | else: 36 | print "We still can't decide." 37 | 38 | # if "people is greater than buses" 39 | if people > buses: 40 | # print string 41 | print "Alright, let's just take the buses." 42 | 43 | # else 44 | else: 45 | # print string 46 | print "Fine, let's stay home then." 47 | 48 | # Extra credit 49 | # 1.a. elif means "else if" - if the previous "if" statement is False, python checks if this elif statement is True. If it is, the code block under it will be evaluated. 50 | # 1.b. else means "else" - if the previious "if (or elif)" statement is False, python evaluates the code block under the "else" statement 51 | -------------------------------------------------------------------------------- /ex31.py: -------------------------------------------------------------------------------- 1 | print "You enter a dark room with two doors. Do you go through door #1 or door #2?" 2 | 3 | door = raw_input("> ") 4 | 5 | if door == "1": 6 | print "There's a giant bear here eating a cheese cake. What do you do?" 7 | print "1. Take the cake." 8 | print "2. Scream at the bear." 9 | print "3. Punch the bear in the face." 10 | 11 | bear = raw_input("> ") 12 | 13 | if bear == "1": 14 | print "The bear eats your face off. Good job!" 15 | elif bear == "2": 16 | print "The bear eats your legs off. Good job!" 17 | elif bear == "3": 18 | print "You knock the bear out!" 19 | print "When the bear falls over, you see a passage behind him. You go through it." 20 | print "It's dark in here, and smells kind of funny. What do you do?" 21 | print "1. Light a match." 22 | print "2. Feel for a lightswitch." 23 | print "3. Turn on your mining hat." 24 | 25 | light = raw_input("> ") 26 | 27 | if light == "1": 28 | print "Whoops, looks like that smell was a gas leak. KABOOM!" 29 | elif light == "2": 30 | print "As you feel along the wall for a switch, you run into an electric fence. ZAP!" 31 | elif light == "3": 32 | print "Smart! You find the exit and head home." 33 | else: 34 | print "You huddle up on the floor and die." 35 | else: 36 | print "Well, doing %s is probably better. Bear runs away." % bear 37 | 38 | elif door == "2": 39 | print "You stare into the endless abyss at Cthuhlu's retina." 40 | print "1. Blueberries." 41 | print "2. Yellow jacket clothespins." 42 | print "3. Understanding revolvers yelling melodies." 43 | 44 | insanity = raw_input("> ") 45 | 46 | if insanity == "1" or insanity == "2": 47 | print "Your body survives powered by a mind of jello. Good job!" 48 | else: 49 | print "The insanity rots your eyes into a pool of muck. Good job!" 50 | 51 | else: 52 | print "You stumble around and fall on a knife and die. Good job!" -------------------------------------------------------------------------------- /ex32.py: -------------------------------------------------------------------------------- 1 | the_count = [1, 2, 3, 4, 5] 2 | fruits = ['apples', 'oranges', 'pears', 'apricots'] 3 | change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] 4 | 5 | # this first kind of for-loop goes through a list 6 | for number in the_count: 7 | print "This is count %d" % number 8 | 9 | # same as above 10 | for fruit in fruits: 11 | print "A fruit of type: %s" % fruit 12 | 13 | # also we can go through mixed lists too 14 | # notice we have to use %r since we don't know what's in it 15 | for i in change: 16 | print "I got %r" % i 17 | 18 | # we can also build lists, first start with an empty one 19 | elements = [] 20 | 21 | # then use the range function to do 0 to 20 counts 22 | elements = range(0, 6) 23 | 24 | # now we can print them out too 25 | for i in elements: 26 | print "Element was: %d" % i 27 | 28 | # Other list functions: append, extend, insert, remove, pop, index, count, sort, reverse -------------------------------------------------------------------------------- /ex33.py: -------------------------------------------------------------------------------- 1 | def add_numbers(max, step=1): 2 | i = 0 3 | numbers = [] 4 | while i < max: 5 | print "At the top i is %d" % i 6 | numbers.append(i) 7 | 8 | i = i + step 9 | print "Numbers now: ", numbers 10 | print "At the bottom i is %d" % i 11 | 12 | return numbers 13 | 14 | numbers = add_numbers(10, 5) 15 | 16 | print "The numbers: " 17 | 18 | for num in numbers: 19 | print num -------------------------------------------------------------------------------- /ex35.py: -------------------------------------------------------------------------------- 1 | from sys import exit 2 | 3 | prompt = "==> " 4 | 5 | def gold_room(): 6 | """Describe gold room and take user input.""" 7 | print "This room is full of gold. How much do you take?" 8 | 9 | while True: 10 | try: 11 | next = raw_input(prompt) 12 | how_much = int(next) 13 | break 14 | except ValueError: 15 | print "That's not a number... try again!" 16 | 17 | if how_much < 50: 18 | print "Nice, you're not greedy, you win!" 19 | exit(0) 20 | else: 21 | dead("You greedy bastard!") 22 | 23 | 24 | def bear_room(): 25 | """Describe bear room and take user input.""" 26 | print "There is a bear here." 27 | print "The bear has a bunch of honey." 28 | print "The fat bear is in front of another door." 29 | print "How are you going to move the bear?" 30 | bear_moved = False 31 | 32 | while True: 33 | next = raw_input(prompt) 34 | 35 | if next == "take honey": 36 | dead("The bear looks at you and then pimp slaps your face off.") 37 | elif next == "taunt bear" and not bear_moved: 38 | print "The bear has moved from the door. You can go through it now." 39 | bear_moved = True 40 | elif next == "taunt bear" and bear_moved: 41 | dead("The bear gets pissed off and chews your crotch off.") 42 | elif next == "open door" and bear_moved: 43 | gold_room() 44 | else: 45 | print "I got no idea what that means." 46 | 47 | def cthulu_room(): 48 | """Describe Cthulu room and take user input.""" 49 | print "Here you see the great evil Cthulu." 50 | print "He, it, whatever stares at you and you go insane." 51 | print "Do you flee for your life or eat your head?" 52 | 53 | next = raw_input(prompt) 54 | 55 | if "flee" in next: 56 | start() 57 | elif "head" in next or "eat" in next: 58 | dead("Well that was tasty!") 59 | else: 60 | cthulu_room() 61 | 62 | 63 | def dead(why): 64 | """Print out death message and exit the program.""" 65 | print why, "Good job!" 66 | exit(0) 67 | 68 | def start(): 69 | """Describe game start and take user input.""" 70 | print "You are in a dark room." 71 | print "There is a door to your right and left." 72 | print "Which one do you take?" 73 | 74 | next = raw_input(prompt) 75 | 76 | if next == "left": 77 | bear_room() 78 | elif next == "right": 79 | cthulu_room() 80 | else: 81 | dead("You stumble around the room until you starve.") 82 | 83 | 84 | start() -------------------------------------------------------------------------------- /ex4.py: -------------------------------------------------------------------------------- 1 | # We have 100 cars 2 | cars = 100 3 | 4 | # You can fit 4 people in a car 5 | space_in_a_car = 4 6 | 7 | # We have 30 drivers 8 | drivers = 30 9 | 10 | # We have 90 passengers 11 | passengers = 90 12 | 13 | # After all of the drivers are in cars, we have "cars not driven" left over 14 | cars_not_driven = cars - drivers 15 | 16 | # The number of cars driven equals the number of drivers 17 | cars_driven = drivers 18 | 19 | # Carpool capacity is the number of cars driven multiplied by the space in each car 20 | carpool_capacity = cars_driven * space_in_a_car 21 | 22 | # The average number of passengers in each car equals the number of passengers divided by the number of cars available 23 | average_passengers_per_car = passengers / cars_driven 24 | 25 | 26 | print "There are", cars, "cars available." 27 | print "There are only", drivers, "drivers available." 28 | print "There will be", cars_not_driven, "empty cars today." 29 | print "We can transport", carpool_capacity, "people today" 30 | print "We have", passengers, "to carpool today." 31 | print "We need to put about", average_passengers_per_car, "in each car." 32 | 33 | # Extra credit: The error was caused by a misspelling of carpool_capacity (car_pool_capacity) 34 | # 4.0 is used to convert to floating point number 35 | 36 | """ 37 | >>> i = 40 38 | >>> x = 234 39 | >>> j = 23.4 40 | >>> i * x / j 41 | 400.0 42 | """ -------------------------------------------------------------------------------- /ex5.py: -------------------------------------------------------------------------------- 1 | name = 'Zed A. Shaw' 2 | age = 35 # not a lie 3 | height = 74 # inches 4 | height_in_cm = height * 2.54 5 | weight = 180 # lbs 6 | weight_in_kg = weight * 0.45359237 7 | eyes = 'Blue' 8 | teeth = 'White' 9 | hair = 'Brown' 10 | 11 | print "Let's talk about %r." % name 12 | print "He's %r inches tall (%r cm)." % (height, height_in_cm) 13 | print "He's %r pounds heavy (%r kg)." % (weight, weight_in_kg) 14 | print "Actually that's not too heavy." 15 | print "He's got %r eyes and %r hair." % (eyes, hair) 16 | print "His teeth are usually %r depending on the coffee." % teeth 17 | 18 | # this line is tricky, try to get it exactly right 19 | print "If I add %r, %r, and %r I get %r." % ( 20 | age, height, weight, age + height + weight) 21 | 22 | # All formatting characters: %d, %i, %o, %u, %x, %X, %e, %E, %f, %F, %g, %G, %c, %r, %s -------------------------------------------------------------------------------- /ex6.py: -------------------------------------------------------------------------------- 1 | # Assign the string with 10 replacing the formatting character to the variable 'x' 2 | x = "There are %d types of people." % 10 3 | 4 | # Assign the string "binary" to the variable 'binary' 5 | binary = "binary" 6 | 7 | # Assign the string "don't" to the variable 'do_not 8 | do_not = "don't" 9 | 10 | # Assign the string with the variables binary and do_not replacing the formatting characters to 'y' 11 | y = "Those who know %s and those who %s." % (binary, do_not) # Two strings inside of a string 12 | 13 | # Print "There are 10 types of people." 14 | print x 15 | 16 | # Print "Those who know binary and those who don't." 17 | print y 18 | 19 | # Print "I said: 'There are 10 types of people.'" 20 | print "I said: %r." % x # One string inside of a string 21 | 22 | # Print "I also said: 'Those who know binary and those who don't.'." 23 | print "I also said: '%s'." % y # One string inside of a string 24 | 25 | # Assign boolean False to 'hilarious' 26 | hilarious = False 27 | 28 | # Assign the string with an unevaluated formatting character to 'joke_evaluation' 29 | joke_evaluation = "Isn't that joke so funny?! %r" 30 | 31 | # Print "Isn't that joke so funny? False" 32 | print joke_evaluation % hilarious # One string inside of a string 33 | 34 | # Assign string to 'w' 35 | w = "This is the left side of..." 36 | 37 | # Assign string to 'e' 38 | e = "a string with a right side." 39 | 40 | # Print concatenation of 'w' and 'e' 41 | print w + e # Using + with two strings attached the second string to the end of the first string -------------------------------------------------------------------------------- /ex7.py: -------------------------------------------------------------------------------- 1 | # prints the string 2 | print "Mary had a little lamb." 3 | 4 | # prints the string with the 'snow' inserted where the formatting character is 5 | print "Its fleece was white as %s." % 'snow' 6 | 7 | # prints the string 8 | print "And everywhere that Mary went." 9 | 10 | # prints 10 periods 11 | print "." * 10 # what'd that do? 12 | 13 | # sets end1 to "C" 14 | end1 = "C" 15 | # sets end2 to "h" 16 | end2 = "h" 17 | # sets end3 to "e" 18 | end3 = "e" 19 | # sets end4 to "e" 20 | end4 = "e" 21 | # sets end5 to "s" 22 | end5 = "s" 23 | # sets end6 to "e" 24 | end6 = "e" 25 | # sets end7 to "B" 26 | end7 = "B" 27 | # sets end8 to "u" 28 | end8 = "u" 29 | # sets end9 to "r" 30 | end9 = "r" 31 | # sets end10 to "g" 32 | end10 = "g" 33 | # sets end11 to "e" 34 | end11 = "e" 35 | # sets end12 to "r" 36 | end12 = "r" 37 | 38 | # watch that comma at the end. try removing it to see what happens 39 | # prints the concatenation of end1 through end6 - the comma causing python to not add a line-break 40 | print end1 + end2 + end3 + end4 + end5 + end6, 41 | # 42 | print end7 + end8 + end9 + end10 + end11 + end12 -------------------------------------------------------------------------------- /ex8.py: -------------------------------------------------------------------------------- 1 | # create a variable called formatter, which is a string made of 4 formatting characters 2 | formatter = "%r %r %r %r" 3 | 4 | # prints formatter with 4 digits 5 | print formatter % (1, 2, 3, 4) 6 | # prints formatter with 4 strings 7 | print formatter % ("one", "two", "three", "four") 8 | # prints formatter with 4 Booleans 9 | print formatter % (True, False, False, True) 10 | # prints formatter with itself, 4 times 11 | print formatter % (formatter, formatter, formatter, formatter) 12 | # prints formatter with 4 strings 13 | print formatter % ( 14 | "I had this thing.", 15 | "That you could type up right.", 16 | "But it didn't sing.", 17 | "So I said goodnight." 18 | ) 19 | 20 | # It uses both double and single quotes because one of the lines has an apostrophe in it -------------------------------------------------------------------------------- /ex9.py: -------------------------------------------------------------------------------- 1 | # Here's some new strange stuff, remember type it exactly. 2 | 3 | # Set a variable called days to a string with shortened day names 4 | days = "Mon Tue Wed Thu Fri Sat Sun" 5 | 6 | # Set a variable called months to a string with shortened month names, separated by \n (newline) characters 7 | months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" 8 | 9 | # print a string, along with days 10 | print "Here's the days: ", days 11 | 12 | # print a string, along with months 13 | print "Here's the months: ", months 14 | 15 | # print a long string with line breaks 16 | print """ 17 | There's somethings going on here. 18 | With the three double-quotes. 19 | We'll be able to type as much as we like. 20 | """ --------------------------------------------------------------------------------