Day 26 Wrap-Up

Today we began our discussion on File Input and Output using Python. Here’s an overview of what we covered:

  1. All of the programs that you have been writing so far have reset themselves each time they run. This is because the data associated with the program (i.e. your variables) are stored in your computer’s memory (RAM)
  2. RAM is a volatile place – it serves as a computer’s short term memory. RAM that is being used by a program is cleared when the program stops running. If programs are to retain data between executions then we must find a more permanent way to store and save information for future use.
  3. A computer almost always has at least one type of long-term storage device at its disposal (usually some kind of hard drive) — we can use the long term storage capabilities of a computer to store data in the form of a file.
  4. Once we save a file it will remain on the long term storage device after the program is finished running, and can be accessed and retrieved later on. This is a pretty common technique and is used by almost all programs that need to keep track of some kind of information between executions.
  5. There are two types of files we can work with – “text files” and “binary files.”
  6. Text files are files that contain data that is encoded as text (i.e. ASCII or Unicode characters). Data stored in a text file is visible and can be read by a program that is designed to view / edit textual data (i.e. a word processing program). We will be working exclusively with Text Files this semester.
  7. Binary files contain data that is not encoded as a text format – they are intended to be read by other programs and not by humans directly. Binary files appear as “gibberish” when viewed via a word processing program. An example of a binary file would be a movie or image file – if you were to open it up in a word processing program you would see nothing but intelligible characters.
  8. Every file must have its own filename, which is nothing more than a label that allow us to uniquely identify groupings of data that exist on a long term storage device. On most operating systems a filename comes with a “file extension” which allows you to quickly tell what kind of data is stored inside that file. Extensions come after the last period in a file (i.e. “mydocument.doc” – “doc” is the file extension). File extensions tell the operating system what kind of data is stored in the file and allows the OS to select an appropriate program to open a particular file.
  9. To begin working with files in Python you need to first create a “File Object.” A File Object is a special variable that serves as a connection between your program and the operating system. File Objects tell the OS the name of a file that you wish to work with along with what you want to do to that file (write, read, append, etc). Here’s how to open up a file object in Python that tells the operating system that you want to write to file called “myfile.txt”:
    file_object = open("myfile.txt", "w")
  10. The open() function takes two arguments – the name of a file and a “mode” operation. Both of these arguments are strings. Python supports three file modes:
    1. “w” – Write Mode. Open up a file so you can write information to that file. If the file does not exist it will be created. If the file does exist it will be overwritten.
    2. “a” – Append Mode. Open up a file so you can write information to that file. If the file does not exist it will be created. If the file does exist we will simply “append” any new data to the end of the file.
    3. “r” – Read Mode. Opens upa file so that you can read its contents.
  11. You can write data to a file that has been opened for writing by using the write() function. Here’s an example:
    # open up a file object for writing
    file_object = open('myfile.txt', ‘w’)
    
    # store the string 'craig' in the file
    file_object.write('craig')
    
    # store the string 'hello' in the file
    file_object.write('hello')
    
    # close the file when you’re done
    file_object.close()
  12. If you were to look at the file that gets created by the source code above you would see that both strings (‘craig’ and ‘hello’) are concatenated on the same line. The write() function simply takes its data and stores it in the file – it does not provide any formatting or extra characters to help delimit your data.
  13. You want to try and avoid writing files that concatenate all of your data into one long line of unintelligible text. This is bad practice since it can be very difficult – or impossible – to extract out your data later on. One way to separate data in a text file is by splitting out your data into multiple lines using the “n” escape character. This allows you to store a single value on each line of your text file, which will make things a lot easier for you later on when you need to read your file and perform some kind of operation on the data contained within. Here’s an example program that we wrote in class that stores a username and password in a file – each item will be stored on its own line.
    # ask the user for their username
    # and password
    username = input("What's your username: ")
    password = input("What's your password: ")
    
    # store the username and password
    file_object = open("security.txt", "w")
    
    # send the information into the file
    file_object.write(username + "n")
    file_object.write(password + "n")
    
    # print a confirmation
    print ("Your account has been set up")
    
    # close the file
    file_object.close()
  14. You can read data contained inside a file once you have created a file object and have opened a file for reading using the read() function. Here’s an example:
    myvar = open("test.txt", "r”)
    alldata = myvar.read()
    print (alldata)
    myvar.close()
  15. The read() function extracts all data from a file as a string and returns a string. This string must be further processed before it can actually be used, usually through using the string “split” method.
  16. Formatting data that you store in an external file is up to you, and you can choose any layout convention you’d like.  However, many programmers design their files such that data is laid out across multiple lines (using a “n” character) in order to separate important pieces of data.
  17. The readline() function can be used to read a single line of data (until the next “n” character). The function returns a string that contains the requested information (including the “n” character). Your file object keeps track of its position inside a file using a “read position” indicator – it will remember the last line you read using the readline() function and will let you read lines sequentially if you continually call the readline() function. Here’s an example:
    line1 = file_object.readline()
  18. You can also use the readlines() method to read all lines from a file object directly into a Python list.  Here’s an example:
    filelist = file_object.readlines()
    print (filelist)
    
    >> [ 'I'm on line 1!n', 'I'm on line 2!n' ]
  19. You can use the special “rstrip” function that is built into the String data type to remove certain characters. “rstrip” takes one argument – a string – and returns a string version of itself with all instances of the supplied character pattern removed. You can use the “rstrip()” function to extract all instances of the “n” character by doing the following:
    for x in range(len(filelist)):
         filelist[x] = filelist[x].rstrip("n")
  20. Here’s an example that puts all of this together — in this program we open up the “security.txt” file that we created earlier and extract the username and password stored inside. Then we ask the user for a username & password combo and test to see if the two are the same.
    # open up a connection to our file
    file_object = open("security.txt", "r")
    
    # grab the username & password as a list
    filelist = file_object.readlines()
    
    # remove the line breaks
    for x in range(len(filelist)):
         filelist[x] = filelist[x].rstrip("n")
    
    # store the username and password in new variables to make
    # our program more "readable"
    username_from_file = filelist[0]
    password_from_file = filelist[1]
    
    # close the file
    file_object.close()
    
    # now that we have the username and password
    # ask the current user for their info
    # if it matches what's in the file we cna let them in
    # otherwise they can't log in
    username = input("username: ")
    password = input("password: ")
    
    # check to see if they match
    if username == username_from_file and password == password_from_file:
        print ("you're in!")
    else:
        print ("sorry, wrong info")
  21. When you are reading data from a file you will need to convert strings to ints or floats if you want to perform calculations on them. This is identical to what we have to do when using the input() function to ask the user to enter a string. Note that the int() and float() functions automatically remove line break characters for you. Here’s an example that opens up a text file called “testscores.txt” — this file contains the following information:
    craig
    100
    82
    74

    And here is how we can work with the file by treating the score values as integers:

    # open up the file
    file_object = open("testscores.txt", "r")
    
    # read in each line manually (without putting data into a
    # list first)
    studentname = file_object.readline()
    studentname = studentname.rstrip("n")
    score1 = int(file_object.readline())
    score2 = int(file_object.readline())
    score3 = int(file_object.readline())
    
    # print out data
    print (studentname)
    
    average = (score1 + score2 + score3) / 3
    
    print ("average: ", average)
  22. You can also use repetition structures to write large amounts of data into a file. Here’s an example that writes out a series of price values that are entered by the user:
    # open up our file
    file_object = open("prices.txt", "a")
    
    keepgoing = True
    
    while keepgoing == True:
    
        # ask the user for a price
        price = float(input("give me a price: "))
    
        if price > 0:
    
            # store the price
            file_object.write(str(price) + "n")
    
        else:
    
            # end the loop
            keepgoing = False
    
    # close the file
    file_object.close()
  23. Most times you won’t know how many lines of data exist in a text file. One strategy for dealing with these kinds of files is to use a while loop in combination with the readline() function. The readline() function will return an empty string (‘’) when it has reached the end of the file. Here’s an example that prints out the average price of the items entered by the previous program:
    # accum variables
    total = 0
    num_prices = 0
    
    # open up our file
    file_object = open("prices.txt", "r")
    
    # continually real from the file as long as there
    # is data to be read
    keepgoing = True
    
    while keepgoing == True:
    
        # read the next line from the file
        nextline = file_object.readline()
    
        # test to see if we have reached the end of the file
        if nextline == '':
            keepgoing = False
        else:
            nextline = nextline.rstrip("n")
            nextline = float(nextline)
    
            # accum variables
            total = total + nextline
            num_prices += 1
    
            print (nextline)
    
    # print out summary
    print ("average: ", total / num_prices)
    
    # close the file
    file_object.close()
  24. Here are some additional practice problems that we went over in class. Problem #1 – Write a “matchmaking” program that asks the user to enter in their favorite color and their favorite food. Store the result in a text file.
    # open up the file
    file_object = open("match.txt", "w")
    
    # ask the user their fav color
    color = input("Fav color: ")
    
    # ask the user their fav food
    food = input("Fav food: ")
    
    # write the info to the file
    file_object.write(color + "n")
    file_object.write(food + "n")
    
    # close the file
    file_object.close()
  25. Problem #2 – Interface with your matchmaking text file and ask a second user for a favorite color and favorite food. Compare the results – if they get 0/2 questions correct, they are not a match! ½, they might be a match. 2/2, they are definitely a match!
    # get the info from the file that we will
    # be using for comparison purposes
    file_object = open("match.txt", "r")
    
    # get the fav food and fav color
    color = file_object.readline()
    food = file_object.readline()
    
    # get rid of the line breaks
    color = color.rstrip("n")
    food = food.rstrip("n")
    
    # close the file
    file_object.close()
    
    # ask the current user what their prefs are
    usercolor = input("What's your fav color? ")
    userfood = input("What your fav food? ")
    
    # compare
    points = 0
    
    if usercolor == color:
        points +=1
    if userfood == food:
        points +=1
    
    print (points/2, "percent match!")
  26. Problem #3 – Open up a text file named “drawing.txt” for read access. Using the turtle graphics library, extract the coordinates contained in the file and draw the resulting picture. Use the turtle.goto() function to move the turtle from point to point.
    import turtle
    
    turtle.setup(500,500,0,0)
    
    # open up the file
    file_object = open("drawing.txt", "r")
    
    # go through each line
    keepgoing = True
    
    while keepgoing == True:
    
        # read the x and y coordinate
        x = file_object.readline()
    
        # are we at the end?
        if x == '':
            keepgoing = False
        else:
            # read the y coordinate
            y = file_object.readline()
    
            # convert to floats
            x = float(x)
            y = float(y)
    
            # draw this to the screen
            turtle.goto(x,y)
    
    file_object.close()

    “drawing.txt”

    -120.0
    -95.0
    -76.0
    22.0
    -13.0
    185.0
    66.0
    24.0
    114.0
    -103.0
    24.0
    -103.0
    23.0
    -187.0
    -38.0
    -186.0
    -39.0
    -101.0
    -120.0
    -96.0

Day 25 Wrap-Up

Today we covered the following topics:

  1. We discussed a new technique that allows us to “split” a string up into smaller parts so that we can “unpack” it into a list.  For example, consider the following string:
    x = “Craig,Professor,NYU”
  2. This string contains three pieces of data – a name, a title and an employer. The data has been “packaged” into a single string in a meaningful way (i.e. we know that the comma character separates the different data points)
  3. We can use a technique called “splitting” to “unpack” a string and extract its individual values. The trick is to isolate a “separator” character that can be used to delimit the data you want to extract. Here’s an example:
    mystring = “Craig,Professor,NYU”
    splitstring = mystring.split(“,”)
    print (splitstring)
    
    >> [‘Craig’, ‘Professor’, ‘NYU’]
  4. General Algorithm:
    1. Obtain a string that contains data organized in a certain way
    2. Split the string into a list
    3. Process the list using list mechanics
  5. We then wrote a program that processed a string that contained grade information and generated the average score for the student, the highest and lowest scores, and the average with the lowest score “dropped.”
    scores = "95,100,67,33,88"
    
    # convert scores into a list
    mylist = scores.split(",")
    
    # convert all values int he list to ints
    for x in range(len(mylist)):
        mylist[x] = int(mylist[x])
    
    total = 0
    
    # get info from list
    for x in mylist:
        total += x
    
    print ("Average:", total / len(mylist))
    print ("Best:", max(mylist))
    print ("Word:", min(mylist))
    
    # dropping the lowest
    newtotal = total - min(mylist)
    newaverage = newtotal / (len(mylist) - 1)
    
    print ("Average with dropped:", newaverage)
  6. Next we moved on to talk about how we can obtain data from outside of our Python programs. When writing a large program you generally won’t be “hard coding” data into your programs – most times you will be processing information that comes to you in the form of user input or via an external data source.
  7. One way to easily get information into Python from the “outside world” is to initiate a connection to the Web and obtain data via a URL.  Here is the general algorithm for doing this:
    1. Initiate a web connection to an external data source
    2. Obtain the data that exists at that location as a String
    3. “Clean up” the string into a meaningful form that your program can understand. This usually involves making sure that the character set used in the external file is one that your computer can handle.
    4. Parse your string into a list
    5. Process this list and generate output
  8. You can initiate web connections using the “urllib” module. Example:
    import urllib.request
    
    # where should we obtain data from?
    url = “http://www.nyu.edu”
    
    # initiate request to URL
    response = urllib.request.urlopen(url)
    
    # read data from URL as a string
    data = response.read().decode(‘utf-8’)
    print (data)
  9. Some notes of caution:
    1. The web is a volatile place – servers can go up and down at a moment’s notice, and your connection to the network may not always be available.
    2. You need to be prepared to deal with connection errors in your programs.
    3. You can use the try / except / else suite to help “catch” errors before they crash your program. For example:
      try:
           # initiate request to URL
           response = urllib.request.urlopen(url)
      except:
           print (“Looks like the request failed!”)
      else:
           # read data from URL as a string
           data = response.read().decode(‘utf-8’);
  10. Next we wrote an interactive “voting” application that lets users submit votes via SMS text messages.  For this project built a system that will did the following:
    1. Allow users to “vote” for their favorite Python language construct (“if”, “while”, “for”, “try”, etc)
    2. Voting will happen via SMS message
    3. Votes will be tabulated in real-time and displayed to the audience
  11. SMS Voting can be “outsourced” to a company that can handle text messages and deliver them to you as string data.  We used a free service called “TextMarks” to do this.  We set up a new keyword with TextMarks (the keyword “Python”) and users can text their vote to by doing the following:
    1. Text to:  4144
    2. Message:  Python *your vote*
    3. … where “*your vote*” is the piece of text you want to send to the voting application.
  12. Here is how we setup our keyword on their site – the important piece of this is where we want to redirect the data once it has been texted into the system.TextMarks Keyword Setup

    TextMarks Keyword Setup

  13. Next we wrote a small PHP program that was designed to “listen” for communication from TextMarks and store the result in a data file.  The code for this PHP program is below.  If you know any PHP you can pretty easily modify this to run on your own server – just make sure to change the line that points to your data file location and make sure that the data file is writable (i.e. the webserver has the necessary permissions to change that file)
    <?php
    
    # where do we want to store our text messages?
    $file = "/path/to/file/textmarks.txt";
    
    # open up our file for "appending" - this lets us add
    # information to the file
    $fh = fopen($file, 'a') or die("can't open file");
    
    # extract the phone number and the message from our request
    $phone = $_GET['phone'];
    $message = $_GET['message'];
    
    # if the user supplied both a phone number and a message
    # then we can store it in our data file
    if ($phone != "" && $message != "")
       {
       fwrite($fh, $phone . "," . $message . "n");
       fclose($fh);
    
       print "thanks for voting!";
       }
    
    else
       {
       print "no phone or message provided. no vote cast.";
       }
    ?>
  14. Finally we have to process the data using Python.  Here is a how we did this in class:
    import urllib.request
    
    # where should we obtain data from?
    url = "http://www.yourwebsite.com/textmarks.txt" # same URL you used when setting up your keyword
    
    # initiate request to URL
    response = urllib.request.urlopen(url)
    
    # read data from URL as a string
    data = response.read().decode('utf-8')
    data = str.lower(data)
    
    # split the data on the line break
    splitdata = data.split("n")
    
    # go through each message and pull out just the message part
    for message in splitdata:
    
        # split it again so we can get the phone number
        # and the message
        split2 = message.split(",")
    
        print (split2[1])

And here are some programs that we wrote in class that dealt with Strings, Lists and the Web:

# parsing test scores from a String

# define scores
scores = "95,100,67,33,88"

# split them up into a list
scores_list = scores.split(",")

# now we have to convert them to integers
for i in range(len(scores_list)):
    scores_list[i] = int(scores_list[i])

# now we can analyze the scores
print ("Highest score:", max(scores_list))
print ("Lowest score:", min(scores_list))
print ("Average score:", sum(scores_list) / len(scores_list))
print ("Average score with lowest dropped:", ( sum(scores_list)-min(scores_list) ) / ( len(scores_list)-1))
# ask the user to enter in numbers separated by commas

# get input
raw_input = input("Enter in a comma separated list of numbers between 0 and 100: ")

# split into a list
number_list = raw_input.split(",")

# convert to integers
for i in range(len(number_list)):
    number_list[i] = int(number_list[i])

# now analyze the numbers!

# ascending order
print ("Acending order")
for i in range(len(number_list)):
    print (number_list[i])

# decending order
print ("Decending order")
for i in range(len(number_list)-1, -1, -1):
    print (number_list[i])

# sum of the numbers
print ("Sum:", sum(number_list))

# frequency count
frequency = [0] * 101

# iterate over all numbers in the list
for number in number_list:

    # make a note that we have seen this number in our list
    frequency[ number ] += 1

# now print out the frequency list
for i in range(len(frequency)):

    # have we seen this number?
    if frequency[i] > 0:

        print (i, "has been seen", frequency[i], "times")
# morse code converter

# define the code
code = "0,-----,1,.----,2,..---,3,…--,4,....-,5,.....,6,-....,7,--...,8,---..,9,----.,A,.-,B,-...,C,-.-.,D,-..,E,.,F,..-.,G,--.,H,....,I,..,J,.---,K,-.-,L,.-..,M,--,N,-.,O,---,P,.--.,Q,--.-,R,.-.,S,...,T,-,U,..-,V,...-,W,.--,X,-..-,Y,-.--,Z,--.."

# split it apart
splitcode = code.split(",")

# ask the user for a word
word = str.upper(input("word:"))

# analyze the word character by character
for c in word:

    if c in splitcode:

        location = splitcode.index(c)
        print (c, splitcode[location+1])
# getting data from the web - hello, world!

# import the urllib library
import urllib.request

# where should we obtain data from?
url = "http://www.cnn.com"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

print (data)
# most popular boy and girl baby names

# import the urllib library
import urllib.request

# first get the boy names
url = "http://i5.nyu.edu/~cmk380/pythondata/boys_2010.txt"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

# split the data into a list
boynames = data.split("n")

# now get the girl names
url = "http://i5.nyu.edu/~cmk380/pythondata/girls_2010.txt"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

# split the data into a list
girlnames = data.split("n")

# now ask the user to enter in a name
testname = str.upper(input("Enter a name: "))

# see if we know about this name
if testname in boynames:

    # grab its location
    location = boynames.index(testname)

    print (testname, "is #", location+1, "on the list of most popular boy name!")

if testname in girlnames:

    # grab its location
    location = girlnames.index(testname)

    print (testname, "is #", location+1, "on the list of most popular girl name!")
# world series winners

# import the urllib library
import urllib.request

# get winning team data
url = "http://i6.cims.nyu.edu/~kapp/python/WorldSeriesWinners.txt"

# initiate request to URL
response = urllib.request.urlopen(url)

# read data from URL as a string
data = response.read().decode('utf-8')

# split the data into a list
rawdata = data.split("n")

# now we need to analyze the list to extract the winners

# we will need two arrays to keep track of winning teams and how many times they have won
winning_team  = []
winning_count = []

for team in rawdata:

    # do we know about this team yet?
    if team not in winning_team:

        # add it to the lists!
        winning_team.append(team)
        winning_count.append(0)

    # otherwise we do know about this team
    else:

        location = winning_team.index(team)

        # update value
        winning_count[location] += 1

# find the winner!
most_wins = max(winning_count)

best_team = winning_count.index(most_wins)

print ("The team with the most wins is", winning_team[ best_team ], "with", most_wins, "wins!")

Final Exam Review Guide & Practice Problems

I wanted to write up a quick outline of what will be covered on the Final Exam for Introduction to Computer Programming. We will be going over this in detail during class this week as well.

  1. Programming Mechanics
    1. Functions (what are they, using them, arguments, return values, etc)
    2. Variables (what are they, creating them, using them, naming rules, etc)
    3. Reading input from the keyboard with the input() function
  2. Math Expressions
    1. Writing math expressions
    2. Evaluating math expressions
    3. Storing & printing the results of math expressions
  3. Data Types
    1. Strings
    2. Numeric data types
      1. Integers (int)
      2. Floating point numbers (float)
    3. The Boolean data type
    4. The List data type
    5. Mixed type expressions
    6. Type conversion (string to int, int to strings, etc)
  4. Basic String Manipulation
    1. Combining two strings (concatenation)
    2. Multiplying a string (x = ‘hi’ * 5)
    3. Case manipulation (x = str.lower(‘CRAIG’) # converts the string literal ‘CRAIG’ to ‘craig’)
    4. Calculating string length using the len() function
  5. Selection Statements
    1. The structure of an IF statement (IF keyword, condition, colon, indentation)
    2. Writing a condition for an IF statement
    3. Boolean operators (<, >, ==, !=, >=, <=)
    4. Comparing numeric values using Boolean expressions
    5. Comparing string values using Boolean expressions
    6. Using the IF-ELSE statement
    7. Nesting decision structures (IF statements inside other IF statements)
    8. The IF-ELIF-ELSE statement
    9. Logical operators (and, or, not)
  6. Generating random numbers using the random.randint() function
  7. Condition Controlled Loops (while loops)
    1. mechanics & how they work
    2. setting up conditions for a while loop
    3. infinite loops and how to avoid them
    4. sentinels (defining a value that the user enters that causes the loop to end)
    5. input validation loops (asking the user to continually enter a value until that value matches some condition)
  8. Accumulator variables
    1. setting up and using accumulator variables
    2. self referential assignment statements (i.e. counter = counter + 1)
    3. augmented assignment operators (i.e. counter += 1)
  9. Count Controlled Loops (for loops)
    1. mechanics and how they work
    2. iterating over a list (i.e. for x in [1,2,3,4,5]:)
    3. using the target variable in a for loop
    4. nested loops (i.e. loops inside of other loops)
  10. Functions
    1. mechanics and how functions work
    2. the “black box” model
    3. function definitions
    4. arguments
    5. return values
    6. calling a function
    7. local variables (variables that are defined inside a function and can only be accessed inside that function)
    8. passing arguments to your own functions
    9. writing a value returning function (i.e. using the ‘return’ keyword to send a result from your function to the part of your program that called that function)
    10. Input, Processing & Output notation
  11. Modules
    1. Creating a module
    2. Defining functions in a module
    3. Calling functions in a module
  12. Exceptions
    1. Preventing exceptions using selection statements (i.e. using an “if” statement to prevent an error from occuring)
    2. Using the try / except / else suite to test problematic code for an error and “catch” it before it has a chance to crash your program.
  13. Lists
    1. Simple Variables vs. Lists (simple variables can only hold one piece of data, but lists can hold multiple values) – you can think of a list like a “book” and a variable like a “sheet of paper”
    2. Defining lists in Python (i.e. mylist = [1,2,3])
    3. Concatenating lists with the “+” operator
    4. Repeating lists with the “*” operator
    5. Referencing list items using index notation (i.e. mylist[0])
    6. Iterating through a list using a “while” loop
    7. Iterating through a list using a “for” loop
    8. Using the len() function to determine the # of items in a list
    9. Updating the value of an item in a list using bracket notation
    10. Creating empty lists
    11. Finding an item in a list using the “in” operator
    12. Adding items to a list using the append method
    13. Sorting items in a list using the sort method
    14. Reversing items in a list using the reverse method
    15. Finding the position of an item in a list using the index method
    16. Inserting an item in a list at a specific index using the insert method
    17. Finding the largest and smallest values in a list using the min and max methods
    18. Totaling the values of all elements in a list using an accumulator variable
    19. Removing an item from a list using the remove method
    20. Storing lists in files
    21. Reading lists from files using the readlines method
  14. Strings Manipulation
    1. Iterating through all characters in a string using a for loop
    2. Indexing a specific character in a string using bracket notation
    3. Iterating through all characters in a string using a while loop
    4. String immutability (you can’t change a string using bracket notation like you would change a list element)
    5. Testing a string for substrings using the “in” operator
    6. Detecting character types in a string using the built-in string testing methods (isdigit, isalpha, isalnum, islower, isupper, isspace)
    7. Splitting a string into a list using the “split” method
  15. File Input & Output
    1. Opening a file for writing
    2. Opening a file for reading
    3. Writing data to a file
    4. Delimiters (separating data in a file)
    5. Reading data from a file using the read() method
    6. Reading data from a file into a list
    7. Processing data stored in a file
  16. Dictionaries
    1. What is a Dictionary?
    2. Basic usage of Dictionaries
    3. Differences between Lists and Dictionaries
    4. Knowing when to use which data type (lists for numerically indexed data, dictionaries for string-indexed data)

The final exam will consist of approximately 8 questions divided into three types – Evaluation, Trace the Output and Programming Challenges.

Also, as promised, the Final Exam will come with a comprehensive command listing of every function, method and language structure that we covered this semester. You can download a copy of this listing here: Python_Command_Index

Sample Problems

Evaluate the following expressions. Note the data type that gets generated by each expression as well.  You can check the solutions to these problems by copying and pasting them into IDLE to see how Python reacts.

Expression
‘a’ + ‘b’
‘a’ * 5
5 * ‘a’ + 5 * ‘b’
[1] + [2]
['a', 'b'] + ['c', 'd']
[2] * 4 + [4] * 2
[1,2,3] + 3 * [4]
99 % 3
5 + (10 % 3)
100 > 5
100 > 5 and 100 < 99
1 > 2 or 2 > 1
not (1 > 2) and not (3 < 3)
(True or False) and (False and True)
x = ‘giraffe’
x[2] # evaluate the result of this line of code
x = ‘G+I+R+A+F+F+E’
x.split(“+”) # evaluate the result of this line of code
‘cat’ + ‘dog’
‘cat’ > ‘dog’
(99 * 2) < 200 or (100 > 50 * 2)
True or False or False or True

Trace the Output

Trace the output of the following programs (each shaded box below is a separate Python program.)

count = 0

while count < 10:
    print ('Hello')
    count += 1
x = 10
y = 0

while x > y:
    print (x, y)
    x = x - 1
    y = y + 1
keepgoing = True
x = 100

while keepgoing:
    print (x)
    x = x - 10
    if x < 50:
        keepgoing = False
x = 45

while x < 50:
    print (x)
for x in [1,2,3,4,5]:
    print (x)
for p in range(1,10):
    print (p)
for z in range(-500,500,100):
    print (z)
x = 10
y = 5

for i in range(x-y*2):
    print ("%", i)
c = 0

for x in range(10):
    for y in range(5):
        c += 1

print (c)
x = [1,2,3]

counter = 0
while counter < len(x):

    print (x[counter] * '%')

    for y in x:
        print (y * '*')

    counter += 1
for x in 'lamp':
    print (str.upper(x))
x = 'one'
y = 'two'

counter = 0
while counter < len(x):
    print (x[counter], y[counter])
    counter+=1
x = "apple,pear,peach"
y = x.split(",")

for z in y:
    print (z)
x = 'apple,pear,peach,grapefruit'
y = x.split(',')

for z in y:
    if z < 'm':
        print (str.lower(z))
    else:
        print (str.upper(z))

Programming Challenges

    1. Write a program that prompts the user to enter in 5 colors. Print out the colors entered in alphabetical orders. Then print out the colors entered in reverse alphabetical order.
    2. Write a program that prompts the user to enter in an unlimited number of colors. Test to make sure that the user did not already enter a color before storing it (i.e. “We’re sorry, but you’ve already added the color ‘yellow’”). When the user types the string “exit” you should stop collecting colors. The print out the colors entered in alphabetical order.
    3. Rewrite the following program using a “for” loop:
      x = [1,2,3,4,5]
      
      counter = 0
      while counter < len(x):
          print (x[counter])
          counter += 1
    4. Rewrite the following program using a “while” loop:
      x = ['a','b','c','d']
      
      for y in x:
          print (y)
    5. Rewrite the following program using a “while” loop:
      x = 'python'
      
      for y in x:
          print (y)
    6. Write a program that lets a user enter in 5 price values. Ensure that the prices entered are greater than 0 (but you can assume they will be numeric and not alphanumeric). Once you have collected all the prices you should print out the following:
      1. The average price
      2. The highest price
      3. The lowest price
      4. If the user spent more than 100, you can give them the lowest priced item for free. In this case you should congratulate them, print out their total bill and average price without the lowest priced item factored in.
    7. Write a function that accepts a single string as an argument. Your function should then determine if the string contains all uppercase characters. If so, return True, and if not return False.
    8. Write a function that accepts a single string as an argument. Convert the string to all lowercase and return the new string.
    9. Write a program that continually prompts the user to enter in a number. Evaluate whether the value entered is a valid number (float or int). If it is, print out the number. If it isn’t, ask the user to try again.  Your program should not crash if the user enters an invalid number (i.e. “twenty” is invalid since it does not contain all numeric digits)
    10. Write a function that generates a random letter from the string ‘BINGO’. Here is a sample running of this program:
      x = generate_bingo_letter()
      print (x)
      
      >> N
      
      x = generate_bingo_letter()
      print (x)
      
      >> O
    11. Take your “bingo” function and write a program that continually generates 5 random letters from the string ‘BINGO’ and print them out along with the attempt number. If all 5 letters spell the word “BINGO” you can end the program and print out a congratulatory message. Here’s a sample running of the program:
      0 GBOBI
      1 OOOIG
      2 NIIIN
      3 BIGNN
      4 OGOIN
      5 BNOGB
      
      .......
      
      3856 BGIIO
      3857 NBONN
      3858 IBNGI
      3859 GNOBN
      3860 BINGO
      Finally, after 3860 tries!
    12. You have been asked to write a “slot machine” program which should randomly arrange three graphical symbols on the screen.  All possible symbols are defined in the following list:
      symbols = ['cherry', 'pear', 'star', 'seven', 'watermelon']

      Write a program that randomly selects three of these symbols and prints them out to the user.  You cannot repeat a symbol (i.e. you cannot select ‘cherry’ twice).  Here’s  are a few sample runnings of the program:

      >> cherry star seven
      
      >> seven watermelon star
      
      >> pear star cherry
    13. Write a program that generates a random series of lottery numbers for the user.  Your program should generate 5 unique random numbers between 1 and 100.  Numbers should not be repeated within a series and should be printed in ascending order.  Here are three sample runnings of the program:
      Your lottery #'s are:
      [19, 61, 62, 78, 99]
      >>> ================================ RESTART
      
      Your lottery #'s are:
      [1, 41, 64, 66, 78]
      >>> ================================ RESTART
      
      Your lottery #'s are:
      [19, 20, 28, 41, 97]

Day 24 Wrap-Up

Today we continued our discussion on lists.  Here’s an overview of what we covered:

  1. We started off by writing a simple program that lets a store manager calculate weekly salary figures for his or her employees.  Here’s the code we wrote:
    # total # of employees
    NUM_EMPLOYEES = 8
    
    # list to hold the employees
    hours = [0] * 8
    
    # get employee info for these employees
    for x in range(NUM_EMPLOYEES):
    
        # get hours worked
        print ("Employee #", (x+1), end='')
    
        temp = float(input(" hours worked: "))
    
        # put the hours into the list
        hours[ x ] = temp
    
    # ask for hourly rate
    rate = float(input("Hourly rate: "))
    
    # go through the list again and compute salary
    for x in range(NUM_EMPLOYEES):
    
        # compute salary
        salary = rate * hours[x]
    
        print ("Employee #", (x+1), "earned", salary)
  2. Next we wrote a larger program that acted as an “inventory management” system for a small grocery store.  We started off by creating a simple program that defined our inventory using three lists.  The position of an item in the ‘products’ list can be used to “look up” the price of the product and how many of those products we have available for sale.  Our first attempt at the program included a simple menu system that gave the user two choices – print out the available inventory for the store or quit the program.  Here’s the code:
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory or (Q)uit: "))
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
  3. Next we extended the program so that we could buy items. When we purchase an item we need to verify that (a) the item exists and (b) that we have enough of those items available for sale. Here’s the code:
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory, (B)uy or (Q)uit: "))
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
    
        # buy mode
        if mode == 'b':
    
            desired_item = str.lower(input("What product? "))
    
            # do we carry this item?
            if desired_item not in products:
    
                print ("Sorry, we don't carry this item.")
    
            # yes, we do carry this!
            else:
    
                # how many do they want to buy
                amount = int(input("How many? "))
    
                # figure out the location of this product in our list
                location = products.index(desired_item)
    
                # make sure we have enough of this stuff to sell
                if inventory[ location ] >= amount:
    
                    # we can make a sale!
                    print ("This will cost you", amount * prices[location])
    
                    # decrease available inventory so we don't sell more of this later on
                    inventory[ location ] -= amount
    
                else:
    
                    print ("Sorry, we don't have enough of those to sell you")
  4. Then we added in the ability to add new products into the system. This feature also makes sure that we aren’t adding duplicate products. Here’s the code:
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory, (B)uy, (A)dd or (Q)uit: "))
    
        # add mode
        if mode == 'a':
    
            # ask the user for a new product name
            newproduct = str.lower(input("New Product Name: "))
    
            # make sure we don't sell this already
            if newproduct in products:
                print ("Sorry, we already sell this")
    
            else:
    
                newprice = float(input("Price: "))
                newamount = int(input("Amount of items: "))
    
                # add the items to our database
                products = products + [newproduct]
                prices.append(newprice)
                inventory.append(newamount)
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
    
        # buy mode
        if mode == 'b':
    
            desired_item = str.lower(input("What product? "))
    
            # do we carry this item?
            if desired_item not in products:
    
                print ("Sorry, we don't carry this item.")
    
            # yes, we do carry this!
            else:
    
                # how many do they want to buy
                amount = int(input("How many? "))
    
                # figure out the location of this product in our list
                location = products.index(desired_item)
    
                # make sure we have enough of this stuff to sell
                if inventory[ location ] >= amount:
    
                    # we can make a sale!
                    print ("This will cost you", amount * prices[location])
    
                    # decrease available inventory so we don't sell more of this later on
                    inventory[ location ] -= amount
    
                else:
    
                    print ("Sorry, we don't have enough of those to sell you")
  5. Finally we added in the ability to remove products from the database.
    products  = ['turkey', 'cranberry', 'stuffing']
    prices    = [4.99, 1.50, 0.99]
    inventory = [10, 5, 3]
    
    # menu system
    while True:
    
        # ask for mode
        mode = str.lower(input("(I)nventory, (B)uy, (A)dd, (R)emove or (Q)uit: "))
    
        # remove mode
        if mode == 'r':
    
            # ask the user for the product to remove
            removeproduct = str.lower(input("What product to remove: "))
    
            # make sure we own this product
            if removeproduct not in products:
    
                print ("Sorry, we don't carry that.")
    
            else:
    
                # figure out the location of this product
                location = products.index(removeproduct)
    
                # delete all of these slots from all lists
                del products[ location ]
                del prices[ location ]
                del inventory[ location ]
    
        # add mode
        if mode == 'a':
    
            # ask the user for a new product name
            newproduct = str.lower(input("New Product Name: "))
    
            # make sure we don't sell this already
            if newproduct in products:
                print ("Sorry, we already sell this")
    
            else:
    
                newprice = float(input("Price: "))
                newamount = int(input("Amount of items: "))
    
                # add the items to our database
                products = products + [newproduct]
                prices.append(newprice)
                inventory.append(newamount)
    
        # quit mode
        if mode == 'q':
            break
    
        # inventory mode
        if mode == 'i':
            print (format("Product", "<21s"), format("Price", "<10s"), format("Amount", "<10s"))
            for x in range(len(products)):
                print (format(products[x], "<21s"), format(prices[x], "<10.2f"), format(inventory[x], "<10d"))
            print()
    
        # buy mode
        if mode == 'b':
    
            desired_item = str.lower(input("What product? "))
    
            # do we carry this item?
            if desired_item not in products:
    
                print ("Sorry, we don't carry this item.")
    
            # yes, we do carry this!
            else:
    
                # how many do they want to buy
                amount = int(input("How many? "))
    
                # figure out the location of this product in our list
                location = products.index(desired_item)
    
                # make sure we have enough of this stuff to sell
                if inventory[ location ] >= amount:
    
                    # we can make a sale!
                    print ("This will cost you", amount * prices[location])
    
                    # decrease available inventory so we don't sell more of this later on
                    inventory[ location ] -= amount
    
                else:
    
                    print ("Sorry, we don't have enough of those to sell you")
  6. Next we wrote a program that generate any number of bouncing graphical boxes on the screen. In this program we use lists to keep track of the object’s position, speed and color. The “trick” to the code below is in the “gameloop” function. We used a method called “turtle.ontimer()” to repeatedly call the same function over and over again (in this case every 30 milliseconds) – we run the function, and then when it completes we call the “turtle.ontimer()” method to schedule another calling of the function in 30ms.
    import turtle
    import random
    
    # ask the user for a number of objects
    numobjects = int(turtle.textinput("Number of objects", "Number of objects"))
    
    # create lists that store the x & y position for each box
    objects_x = []
    objects_y = []
    
    # create lists that store the x & y speed for each box
    objects_sx = []
    objects_sy = []
    
    # create a list that stores the color for each box
    objects_c  = []
    
    # load our lists with random data
    for x in range(numobjects):
    
        # randomize the x & y positions for each box
        objects_x.append(random.randint(-200, 200))
        objects_y.append(random.randint(-200, 200))
    
        # randomize the speeds for each box
        objects_sx.append(random.randint(-1, 1))
        objects_sy.append(random.randint(-1, 1))
    
        # randomize the color for each box - note that we are
        # using a "list inside a list" here - we call these "multidimensional
        # lists" in Python and "multidimensional arrays" in other languages
        objects_c.append([random.random(), random.random(), random.random()])
    
    # our "gameloop" function runs every 30ms and continually redraws the screen
    def gameloop():
    
        # clear the canvas and hide the turtle
        turtle.clear()
        turtle.hideturtle()
    
        # iterate through all of our objects
        # "x" will assume the index value for each item in the objects_x
        # list, which can be used as the index value for all of the other lists
        for counter in range(len(objects_x)):
    
            # pick up the pen
            turtle.penup()
    
            # go to the current object's location
            turtle.goto(objects_x[counter], objects_y[counter])
    
            # put down the pen
            turtle.pendown()
    
            # set the correct fill color & begin filling
            turtle.fillcolor(objects_c[counter][0], objects_c[counter][1], objects_c[counter][2])
            turtle.begin_fill()
    
            # draw a box
            for x in range(4):
                turtle.forward(20)
                turtle.right(90)
    
            # stop filling when we are done drawing the box
            turtle.end_fill()
    
            # move the object based on its speed (stored in the speed lists)
            objects_x[counter] += objects_sx[counter]
            objects_y[counter] += objects_sy[counter]
    
            # bouncing - check to see if the object hit and edge and
            # react accordingly (nudge it back onto the screen and flip
            # its speed so it moves in the opposite direction)
            if objects_x[counter] > 200:
                objects_x[counter] = 199
                objects_sx[counter] *= -1
            if objects_x[counter] < -200:
                objects_x[counter] = -199
                objects_sx[counter] *= -1
            if objects_y[counter] > 200:
                objects_y[counter] = 199
                objects_sy[counter] *= -1
            if objects_y[counter] < -200:
                objects_y[counter] = -199
                objects_sy[counter] *= -1
    
        # once we have redrawn the screen we can force Python to update
        # all graphics
        turtle.update()
    
        # call this function again in 30ms
        turtle.ontimer(gameloop, 30)
    
    #### MAIN PROGRAM ####
    
    # set up our canvas
    turtle.setup(500,500,0,0)
    
    # tell Python not to draw to the screen - we will call the "update" method
    # when we need to draw (makes things a lot faster)
    turtle.tracer(0)
    
    # call our gameloop function to start up the program
    gameloop()
    
    # when the user clicks on the screen we can end the program
    turtle.exitonclick()

Day 23 Wrap-Up

Today we continued our discussing lists and how to use them in Python. Here’s an overview of the new topics that we covered:

  1. We started off talking about the sort() method.  This method takes no arguments and will reorder your list elements so that they appear in ascending order. The list data type contains a related method called the list.reverse() method, which lets you reverse a list. It won’t sort it though, it simply flips the first element with the last element, the second element with the second to last element, etc. Here’s an example:
      mylist = [1,2,3,99]
      print (mylist)
      mylist.reverse()
      print (mylist)
    
      >> [1, 2, 3, 99]
      >> [99, 3, 2, 1]
  2. Next we talked about how to determine the location of an element in a list. We already covered how you can test to see if an item is in a list (using the ‘in’ operator) but we didn’t talk about how to find the position of the element in the list. You can do this by using the list.index() method. This method takes one argument (a value) and returns the integer position of element that contains that value in the list. If no element contains that value then it will raise an exception, so we generally use this method in conjunction with the “in” operator. Here’s an example:
    mylist = ['apple', 'pear', 'peach', 'cake']
    
    if 'banana' in mylist:
        location = mylist.index('banana')
        print ("banana is at position", location)
  3. We then used the list.index() method to write a program that let a user look up a product and its corresponding price. In order to do this we utilized two lists – one list held our product names and one held the product prices. We assumed that position 0 of the first list related to position 0 of the second list. Given this information we were able to write a product lookup system — here’s what we built:
    products = ['fish', 'meat', 'cake']
    prices   = [1.99, 2.99, 3.99]
    
    # get a name from the user
    name = input("Give me a name: ")
    
    # do we have this product?
    if name in products:
    
        # get its position so we can get its price
        location = products.index(name)
    
        # print out the price
        print (name, 'costs', prices[location])
  4. Next we talked about two different ways in which we could remove an item from a list. The first method removes an element based on its content. For example, if you wanted to remove a list element that contains the word ‘cat’, you could do the following
    mylist = ['cat', 'dog', 'pig']
    mylist.remove('cat')
  5. This method will only remove the first instance of the value ‘cat’ from the list. If there were multiple elements that contained the word ‘cat’ the subsequent instances would be unaffected.
  6. The other method to remove an element is to remove it by position. You can use the “del” command to tell Python to completely delete an element from a list that occupies a particular position. Here’s an example:
    mylist = ['cat', 'dog', 'pig']
    del mylist[1]
    print (mylist)
    
    >> ['cat', 'pig']
  7. We then used the “del” command to update our price lookup system to function as a product deletion routine that deletes both a product and its corresponding price from two different lists.
    products = ['fish', 'meat', 'cake']
    prices   = [1.99, 2.99, 3.99]
    
    print (products)
    print (prices)
    
    # find out where the string 'meat' is in the first list
    meat_location = products.index('meat')
    
    # remove location from both lists
    del products[meat_location]
    del prices[meat_location]
    
    print (products)
    print (prices)
  8. Python has two built in functions that let you get the highest and lowest values in a list. They are called “min” and “max” – here’s an example:
    prices = [3.99, 2.99, 1.99]
    biggest = max(prices)
    smallest = min(prices)
    print (smallest, 'up to', biggest)
  9. Finally we wrote a “weight loss” program that used a list to keep track of weight values over the course of a week:
    # how many days?
    numdays = int(input("How many days? "))
    
    # 7 days worth of values
    weight = [0] * numdays
    
    print (weight)
    
    # get weight values
    counter = 0
    while counter < len(weight):
    
        # get this days weight
        w = int(input("Weight for today: "))
    
        # put the weight into the list
        weight[ counter ] = w
    
        # increase counter to go to the next day
        counter += 1
    
    print (weight)
    
    print ("Weight on first day:", weight[0])
    print ("Weight on the last day:", weight[len(weight)-1])
    print ("Change from first to last", weight[0] - weight[len(weight)-1])
    print ("Average weight:", sum(weight) / len(weight))
    print ("Highest weight:", max(weight))
    print ("Lowest weight:", min(weight))
  10. asdfasdf

Day 22 Wrap-Up

Today we began discussing the idea of “lists” in Python. Here’s an overview of what we covered:

  1. So far we have been working with variables, which can be thought of as little “buckets” that can hold a particular piece of data. Variables are incredibly useful, but they can only hold one piece of data at a time. Example:
    x = 5
    y = 5.0
    z = ‘hello’
    q = True
  2. Each of the above variables will hold a single value – an int, float, String or Boolean. There are times when we need to keep track of multiple pieces of data at the same time, and a single variable is limited to holding just one thing at a time.
  3. “Lists” are a built-in data type in Python that are considered a “sequence” objects. Sequence objects have the ability to hold multiple pieces of data at the same time, and we can use a single sequence variable to hold any number of values. In most programming languages we call these “arrays,” but in Python we call them “lists.”
  4. You can create a list in Python by using bracket notation. Example:
    my_list = [1, 2, 3]
  5. The above code will create a new list in Python that holds three integers – 1, 2 and 3 – in that order. Think of a list as a “book” that holds a series of sheets of paper (variables)
  6. Lists can contain any data type that we have covered so far.
    my_list = [‘Craig’, ‘John’, ‘Chris’]
  7. Lists can also mix data types.
    my_list = [‘Craig’, 5.0, True, 67]
  8. You can print the value of a list using the print() function.
    print (my_list)
    >> ['Craig', 5.0, True, 67]
  9. You can also create lists using the range() function. For example, to create a list of all even numbers between 0 and 100 you can do the following:
    even_numbers = list(range(0,100,2))
  10. You can use the repetition operation (“*”) to ask Python to repeat a list, much like how you would repeat a string.
    my_list = [1, 2, 3] * 3
    print (my_list)
    >> [1, 2, 3, 1, 2, 3, 1, 2, 3]
  11. You can use the concatenation operation (“+”) to ask Python to combine lists, much like how you would combine strings.
    my_list = [1, 2, 3] + [99, 100, 101]
    print (my_list)
    >> [1, 2, 3, 99, 100, 101]
  12. In a book you can reference a page by its page number. In a list you can reference an element by its index number. Indexes always start at the number zero and increase to the length of the list – 1.
    my_list = [‘Craig’, ‘John’, ‘Chris’]
    print (my_list[0])
    
    >> Craig
  13. Lists are “mutable,” which means that they can be changed once they have been created.
    my_list = [1, 2, 3]
    print (my_list)
    >> [1,2,3]
    
    my_list[0] = 99
    print (my_list)
    >> [99,2,3]
  14. You can use a while loop to iterate through a list.
    my_list = ['Craig', 'John', 'Chris’]
    index = 0while index < 3:
       print (index, "is", my_list[index])
       index += 1
  15. You will raise an exception if you attempt to access an element outside the range of a list.
    my_list = ['Craig', 'John', 'Chris’]
    print (my_list[4]) # Index doesn’t exist!
  16. You can use the len() function to determine the size of a list. The len() function can take a list as an argument and will return the integer value of the size of the list.
    my_list = ['Craig', 'John', 'Chris’]
    index = 0
    while index < len(my_list):
       print (index, "is", my_list[index])
       index += 1
  17. Here’s an example:
    mylist = [1.99, 2.99, 3.99, 4.99, 5.99]
    
    # loop through list
    counter = 0
    
    while counter < len(mylist):
    
        print (mylist[counter], mylist[counter] *1.07)
    
        counter += 1
  18. You can also use a for loop to iterate through a list. When you do this the target variable of your loop assumes each value of each element of the list in order.
    my_list = [1,2,3]
    for number in my_list:
       print (number)
    
    >> 1
    >> 2
    >> 3
  19. Since you cannot access an element outside of the range of a list it is sometimes necessary to set up a correctly sized list before you begin working with it.
    # create a list of 7 zeros
    daily_sales = [0] * 7
  20. We then wrote a program that uses a list with 7 empty elements to store sales figures for a week.
    # create an empty list filled with zeros
    mylist = [0] * 7
    
    # loop through 7 times
    for x in range(7):
    
        # get sales figure for this day
        sales = float(input("give me a figure: "))
    
        # store the figure at the next spot in the list
        mylist[x] = sales
    
    print (mylist)
  21. Sometimes you need to extract multiple items from a list. Python contains some built in functions that make it easy for you to “slice” out a portion of a list.
    list_1 = ['zero', 'one', 'two', 'three', 'four', 'five’]
    list_2 = list_1[1:3]
    print (list_1)
    print (list_2)
    >> ['zero', 'one', 'two', 'three', 'four', 'five’]
    >> ['one', 'two']
  22. If you omit the start_index in a slice operation, Python will start at the first element of the list. If you omit the end_index in a slice operation, Python will go until the last element of the list. If you supply a third index, Python will assume you want to use a step value. This works the same as the step value you would pass to the range() function.
  23. You can easily find a particular item in a list by using the “in” operator. The “in” operator will return a Boolean value that indicates whether the item exists somewhere in the list.
    my_list = ['pie', 'cake', 'pizza’]
    if 'cake' in my_list:
       print ("I found cake!”)
    else:
       print ("No cake found.")
  24. You have already seen a few ways in which you can add items to lists. You could repeat the list using the “*” operator, or you could concatenate the list using the “+” operator. Another way to add items to a list is to use the “append” method.The append method is a function that is built into the list datatype which allows you to add items to the end of a list.
    mylist = [‘Christine’, ‘Jasmine’, ‘Renee’]
    mylist.append('Kate')
    print (mylist)
    >> [‘Christine’, ‘Jasmine’, ‘Renee’, ‘Kate’]
  25. You can also have Python sort items in a list using the sort() method.
    my_list = ['pie', 'cake', 'pizza’]
    my_list.append('apple’)
    print (my_list)
    my_list.sort()
    print (my_list)
    
    >> ['pie', 'cake', 'pizza', 'apple’]
    >> ['apple', 'cake', 'pie', 'pizza']

Day 21 Wrap-Up

Today we continued our discussion of string manipulation.  Here’s what we covered:

  1. We started off by reviewing how to handle exceptions and basic string manipulation concepts.  See the blog posting for Day 19 for more information on these topics.
  2. We wrote a “Palindrome Tester” program in class to review basic string manipulation concepts.  Just as a review, a palindrome is a word that reads the same backwards as it does forwards. Here’s the code:
    # ask the user for a word
    word = input("Give me a word: ")
    
    # convert it to lowercase - makes things
    # easier when we need to compare the word
    # later on
    word = str.lower(word)
    
    # construct an empty string to hold the
    # backwards version of the word
    backwards = ""
    
    # iterate over each character in the string
    # begin at the end of the string ( len(word) - 1)
    # stop at the beginning of the string (0, but we
    # go to -1 since the range function never reaches
    # its end point)
    # skip -1 each time
    for x in range(len(word)-1, -1, -1):
        # add the character we are looking at to our
        # "backwards" string
        backwards = backwards + word[x]
    
    # compare the two strings - if they are the
    # same then we have a palindrome!
    if word == backwards:
        print ("Palindrome!")
    else:
        print ("Sorry, not a palindrome!")
  3. We then moved on to talk about “string slicing” which is a technique that lets you extract a span of characters from a string into a new string.  You can specify which characters you want to extract using index notation. For example, the following program will extract the first four characters from the string full_name:
    full_name =“John Smith”
    first_name = full_name[0:4]
    print (first_name)
    
    >> John
  4. When you ask Python to slice a string you need to use bracket notation to specify the index range of the characters you wish to extract.  The syntax for this notation is as follows:
    substring = bigstring[start:end:step]
  5. You must supply at least a start or an ending index value when slicing a string, but the step value is always optional.  Substrings created in this way contain all characters starting at the start value specified and continue up to (but do not include) the ending value.
  6. Omitting a starting or ending index value will cause Python to assume you want to start at the beginning of the string (if you omit a start value) or you want to continue slicing to the end of the string (if you omit the end value).  This should remind you a lot of the range function!
  7. You can also specify negative indexes in your slices.  This will allow you to grab a substring from the end of the string rather than from the beginning. For example, this slice will grab the last 5 characters from a string:
    phrase = "This is a test!"
    lastfive = phrase[-5:]
  8. We did some in-class slicing to demonstrate how this works.  Here’s the code we covered:
    word = "Superman sings in the shower."
    
    print (word[0:8])   # Superman
    print (word[9:14])  # sings
    print (word[:5])    # Super
    print (word[9:])    # sings in the shower.
    print (word[-7:])   # shower.
    print (word[0:len(word):3]) # Seasgit or
  9. Next we used string slicing to write a program that would generate NYU Net ID account names for incoming students.  Here are the specificaitons for this program:

     You just accepted a position at NYU’s ITS department and your first task is to write a program that generates student Net IDs for all incoming freshmen.  Net IDs are generated as follows:

    * The first two characters of a student’s first name

    * The first two characters of a student’s last name

    * The last three characters of a student’s N#

    Write a program that asks the user for these three pieces of information (first name, last name and N#) and generate their Net ID

    # get the student's first name
    first = input("First name: ")
    
    # get the student's last name
    last = input("Last name: ")
    
    # get the student's N number
    nnum = input("N Number: ")
    
    # extract the specified characters
    first_two_first = first[0:2]
    first_two_last  = last[0:2]
    last_three_nnum = nnum[-3:]
    
    # make a net ID
    net_id = first_two_first + first_two_last + last_three_nnum
    
    print (net_id)
  10. You can test to see if a substring exists inside of another string by using the “in” operator. For example:
    word = "Jackson James John Chris Tom”
    if "Chris" in word:
         print ("found him!”)
    else:
         print ("can't find Chris")
  11. When you construct an expression with the “in” operator the result will evaluate to a Boolean.  You can also test to see if a string is not in another string by using the “not” keyword in your expression. For example:
    word = "Jackson James John Chris Tom”
    if ”Johnny" not in word:
         print (”No Johnny!”)
    else:
         print (”Johnny is here!")
  12. We illustrated this concept by writing a program that did the following:

    Write a program that asks a user for a username and a password.  Ensure that the user doesn’t use their username as part of their password.

    # obtain a username and password from the user
    username = input("Username: ")
    password = input("Password: ")
    
    # make sure the username was not used in the password
    if username in password:
        print ("That's not secure at all.")
    
    else:
        print ("Password OK")
  13. Next we discussed the idea of a “method,” which can be thought of as a function that belongs to an “object”, and performs some operation on that object. The general syntax for calling a method on an object looks a lot like a function call, with the notable exception that the method call generally does not need to pass an argument to the function specifying which piece of data is being operated on.  Syntax:
    stringvariable.method(arguments)
  14. Strings have a number of built in methods that you can use right away.  For example, there exist a series of string testing methods allow you to determine if certain patterns existing within a given string variable. For example:
    word = '1234’
    if word.isdigit() == True:
         print ("All chars in word are digits!”)
    else:
         print ("Not all chars in word are digits!”)

    In the above example we are calling the “isdigit” method on the string variable “word”. This method returns True if all characters contained in this string are numeric digits (0-9) and False if not.

  15. Here are a list of string testing methods that we covered in class:
    Method     Output
    isalnum()  True if all characters are alphanumeric
    isalpha()  True if all characters are alphabetic
    isdigit()  True if all characters are digits
    islower()  True is all alpha characters are lower
    isspace()  True if all characters are “whitespace”
    isupper()  True if all alpha characters are upper
  16. We used some of these string testing methods to write a basic “password validator” that is based on the following specification:

    Write a program that asks a user to supply a password.  Determine if the password they supplied is valid. Valid passwords are as follows:
    * Contain only alpha numeric characters
    * Contain no spaces
    * Contain at least one uppercase character
    * Contain at least one lowercase character

    # get a password from the user
    password = input("Password: ")
    
    # if the password is made up of only alpha numeric chars
    if password.isalnum() == True:
    
        print ("password is alpha numeric!")
    
        # next check to see if it has uppercase
        # strings
    
        # go through each character
        # keep track of how many uppercase we find
        numupper = 0
    
        for c in password:
    
            # is this uppercase?  if so, increase
            # our accum variable
            if c.isupper() == True:
                numupper += 1
    
        # when we are all done we can see if
        # we have any uppercase characters
        if numupper > 0:
    
            print ("you have at least 1 uppercase in this password!")
    
        else:
            print ("you have no uppercase in this passwrod")
    
    else:
    
        print ("password contains other characters!")
  17. Programs often need to perform search and replace functions on data.  You can find whether a string exists inside another string by using the find() method – for example:
    word = "Like finding a needle in a haystack!”
    location = word.find("needle”)
    print (location)

    The find() method will return the index of the first occurrence of a substring within a string.  If the find() method cannot find the desired substring it will return the integer value -1. We demonstrated how this can be used by writing the following program:

    “He Who Shall Not Be Named”
    Write a program that asks a user to type in a message..  If they use the word “voldemort” anywhere in their message you should warn them to be more careful!

    phrase = input("Give me a phrase: ")
    
    # see if "voldemort" is in the phrase
    location = phrase.find("voldemort")
    
    # if we found it at a postitive location
    # then we can warn the user
    if location > 0:
    
        print ("Don't say it out loud!")

    We also used the “replace” method to replace a string with another string. Here’s the “his” -> “her” pronoun switcher we wrote in class:

    word = "Pat said that his score on his history test was 86"
    
    # replace all his with her
    # note - there is a leading and trailing space in both
    # strings below.  This allows us to replace " his " and not
    # "history"
    newword = word.replace(" his ", " her ")
    
    print (newword)

Day 20 Wrap-Up

Today we introduced two new concepts – exceptions and string manipulation.  Here’s what we discussed:

  1. We started off by discussing exceptions, which can be described as an error condition that causes a program to halt while it’s running. Here’s an example of some code that would cause an exception to occur in your program (you can’t convert the string “Craig” to an integer):
    x = ‘Craig’
    y = int(x)
  2. When an exception occurs Python will generate a “traceback.” Tracebacks give information about the exception that occurred, including the line number that caused the issue and the name of the exception. When this happens, programmers generally say that an exception has been “raised” by the program.  Many times you can avoid exceptions all together by simply adjusting how we code our algorithms. For example, the following code will raise an exception if x = 0 :
    x = int(input(‘give me a number’))
    print (100/x)

    Yet we can avoid the issue by wrapping the potentially dangerous code inside a selection statement.  For example:

    x = int(input(‘give me a number’))
    if x != 0:
         print (100/x)
  3. Python has an exception handling statement that can be used to “catch” exceptions and prevent them from crashing your program Syntax:
    try:
         # questionable code goes here
    except:
         # this block will run if the code in the # ‘try’ block above raised an exception
    else:
         # this block will run if the code in the # ‘try’ block above was successful
  4. You can use the Try / Except / Else suite of statements to validate the data type of inputted data. For example:
    # attempt to get a number from the user
    # know that we can't count on the user
    # to do anything right, so we have
    # to be prepared
    try:
    
        num = int(input("Give me a number: "))
    
    # if there was an error in the try block above
    # then we can respond to it using the except
    # block below
    except:
    
        print ("That didn't work. What did you do?")
    
    # if the try block succeeded, we can do this
    else:
    
        print ("Thanks!  That worked.")
  5. Next we wrote a program that used the try / except / else suite to tackle a real-world data validation problem. In this program we ask the user for a score on a test as well as a total # of points. Both values are validated and the program is designed not to crash even if the user enters in bad data.
    # ask for a test score
    try:
        score = float(input("Score: "))
    
    # if there is a problem
    except:
        print ("Sorry, score must be a valid #.")
    
    # this will happen if no exception was raised
    else:
    
        # ok, now ask for a total # of points
        try:
            points = float(input("Points total: "))
        except:
            print ("Points must be a number")
        else:
            if points == 0:
                print ("you almost got me ...")
            else:
                print (score / points)
  6. We then moved on to talk more about Strings and how they can be further manipulated.   We began by reviewing what we already know about strings, including:
    1. We already know that a String is one of Python’s “primitive” data types that is designed to store textual information.
    2. You cannot perform mathematical operations on a string in the traditional sense (i.e. answer = ‘2’ + ‘2’ will not yield the integer value 4)
    3. You can combine strings using string concatenation:
      fullname = “John ” + “Smith”
    4. You can repeat a string using string repetition:
      lyrics = “jingle bells,” * 2
    5. You can use a String as an operand in a Boolean expression
      if first_name == “Craig” and last_name == “Kapp”:
    6. You can get the length of a string expressed as an integer by using the len() function
      print (len(“Craig”))
  7. Some programming tasks require you to access the individual characters that are contained within a string. One way to do this is to isolate individual characters within a string by using a for loop. When you do this the target variable in your loop will assume each character in your string one at a time. For example:
    for c in “Craig”:
         print (c)
    
    >> C
    >> r
    >> a
    >> i
    >> g
  8. However, with this technique you cannot directly update the a string while iterating over it using a for loop and manipulating its target variable. For example:
    name = ‘Craig’
    for c in name:
         c = ‘Z’
    
    print (name)
    
    >> Craig
  9. In the above example we are simply updating the target variable and not making any changes at all to the original string. You could, however, use a new variable to make a new string that reflects the changes you wish to apply to your string. For example, the following program will create a second string to accomplish what the previous program was trying to do:
    name = ‘Craig’
    newname = ‘’
    for c in name:
         newname += “Z”
    
    print (name)
    print (newname)
    
    >> Craig
    >> ZZZZZ
  10. Now that you can iterate over a string using a for loop it is pretty easy to conceptualize how you could analyze the contents of a string using a series of “if” statements. Here are a few examples we built in class:

    Count the # of “S” characters

    word = "Sally sings in the shower"
    word = str.lower(word)
    
    count = 0
    
    for letter in word:
    
        if letter == "s":
            count += 1
    
    print ("Total S's:", count)

    Remove Vowels

    name    = input("Give me a name: ")
    newname = ""
    
    # remove all vowels:
    for letter in name:
    
        if letter == 'a' or letter == 'e':
            newname = newname + "_"
        else:
            newname = newname + letter
    
    print (name)
    print (newname)

    Count the # of characters

    word = 'Superman sings in the shower'
    counter = 0
    for c in word:
    if c == 's’ or c ==‘S’:
    counter += 1
    print ("There are", counter, "'s' characters in the string", word)
  11. Another way to analyze a string is to use “indexing” notation, which is a way to reference individual elements within a string based on their position
    You can create an index by placing an integer value inside a pair of square brackets after your string variable.

    word = “superman”
    print (word[0])
    
    >> s
  12. Indexes always start with zero. The first character in a string is considered the “0th” element in the string.
    The index of last character in a string is equal to the length of the string – 1. You can use negative integers to identify character positions relative to the end of the string
    Example:

    word = "Superman”
    print (word[-1], word[-2], word[-3])
    
    >> n a m
  13. You will raise an exception if you attempt to access an index of a string that does not exist. For example:
    word = "Superman”
    print (word[10])
    # error!  index 10 doesn’t exist on word
  14. Strings are considered an immutable data type. This means that they cannot be changed once they are created. This may seem counter intuitive, since we have been doing the following since the beginning of the semester:
    word = “superman”
    print (“word is”, word)
    word = “wonder woman”
    print (“word is now”, word)
    
    >> word is superman
    >> word is now wonder woman

    What actually happens “under the hood” is that Python creates a separate string in your computer’s memory and “points” to that string instead of the original one. This means that you cannot change the individual characters within a string using index notation. You will raise an exception if you attempt to do so.

  15. We finished off by writing a string manipulation program in class that served as a one-word “pig latin” translator. Here’s the code:
    # get a word from the user
    word = input("Give me a word: ")
    
    # set up a new word for our pig latin version
    newword = ""
    
    # go through each character in our word, starting at the
    # second character
    for x in range(1, len(word)):
    
        # add the character to our new word
        newword = newword + word[x]
    
    # place the first character of the word at the end of
    # our new world, along with the suffix "ay"
    newword = newword + word[0] + "ay"
    
    print (newword)

Day 19 Wrap-Up

Today we continued talking about how to generate images through the “Turtle Graphics” module that comes with Python. Here’s what we covered:

  1. We began by discussing some new turtle graphics functions, including how to hide and show the turtle cursor. You can use the turtle.hide() function to cause Python to hide the turtle cursor and turtle.show() to turn it back on.
    # hide the turtle
    turtle.hide()
    
    # show the turtle
    turtle.show()
  2. We also discussed ways to speed up the drawing process. By default the speed of the turtle cursor is quite slow. You can speed up drawing by using the turtle.speed() function. This function takes one argument – an integer that indicates how quickly you want the turtle to move. 1 is the slowest speed and 10 is the fastest.
    # speed up the turtle!
    turtle.speed(10)
  3. You can also use the “tracer” method to speed up your drawing. This method tells Python to temporarily pause drawing to the screen — you can then resume drawing at a later date using the turtle.update() function. Here’s an example:
    # tell Python that we should redraw the screen
    # every 0 milliseconds, which means don’t draw
    # anything!
    turtle.tracer(0)
    
    # do your drawing here
    
    # tell Python to redraw the screen
    turtle.update()
  4. We demonstrated how powerful this technique is by having Python draw 1,000 randomly colored boxes on the screen. This would take *hours* if you were to draw it in real time!
    import turtle
    import random
    
    turtle.title("Program X")
    turtle.setup(500, 500, 0, 0)
    
    def polygon(sides, length):
    
        for x in range(sides):
            turtle.forward(length)
            turtle.right(360/sides)
    
    # hide the turtle
    turtle.hideturtle()
    
    # tell python to no longer update the graphics
    turtle.tracer(0)
    
    for x in range(1000):
        # choose a random spot
        xpos = random.randint(-200,200)
        ypos = random.randint(-200,200)
    
        # goto this spot
        turtle.penup()
        turtle.goto(xpos, ypos)
        turtle.pendown()
    
        # generate a random color
        red = random.random() # returns a number between 0 and 1
        green = random.random()
        blue = random.random()
    
        # fill in our shape
        turtle.fillcolor(red, green, blue)
    
        # draw the shape
        turtle.begin_fill()
        polygon(4, 50)
        turtle.end_fill()
    
    # update the screen with our drawing
    turtle.update()
    
    turtle.exitonclick()
  5. You can also have your turtle write text to the screen using the turtle.write() function. This function takes two arguments – a string to print and a font setting. and the turtle will print the text you specify at its current location. Here’s the basic usage of the function:
    turtle.write(”Hi", font=("Arial", 20, "normal"))
  6. You can vary the thickness of the turtle’s pen size by using the turtle.pensize() function. It accepts a single argument (an integer) that represents the width of the pen in pixels.
  7. So far we has been controlling the heading of our turtle using relative commands (i.e. right() and left()). You can also specify an exact heading in degrees by using the turtle.setheading() function. This function takes one argument (a # of degrees) which map to the following directions:
    0 = right
    90 = up
    180 = left
    270 = down
  8. Python even lets you create basic interactive multimedia projects using Turtle Graphics by setting up a series of “listeners” and “callbacks.” For example, you can have the turtle graphic canvas “listen” for when the user strikes a particular key on their keyboard and respond by calling a function. Here’s an example:
    import turtle
    import random
    
    # our generic polygon function
    def polygon(sides, length):
    
        for x in range(sides):
            turtle.forward(length)
            turtle.right(360/sides)
    
    # create a drawing variable
    is_drawing = True
    
    # bound to the "q" key - will draw a random polygon
    def draw_poly():
        polygon(random.randint(4,10), 50)
    
    # bound to the "up" key - turtle up
    def go_up():
        turtle.setheading(90)
        turtle.forward(10)
    
    # bound to the "right" key - turtle right
    def go_right():
        turtle.setheading(0)
        turtle.forward(10)
    
    # bound to the "left" key - turtle left
    def go_left():
        turtle.setheading(180)
        turtle.forward(10)
    
    # bound to the "down" key - turtle down
    def go_down():
        turtle.setheading(270)
        turtle.forward(10)
    
    # bound to the "p" key - toggles pen up/down
    def toggle_pen():
        global is_drawing
    
        # if we are currently drawing then pull
        # up the pen
        if is_drawing == True:
            turtle.penup()
            is_drawing = False
        else:
            turtle.pendown()
            change_pen()
            is_drawing = True
    
    # change the pen color - called whenever we put the pen down
    def change_pen():
        turtle.pensize(random.randint(1,10))
    
        # generate random colors
        red = random.randint(1,100) / 100
        green = random.random()
        blue = random.random()
    
        # assign color
        turtle.pencolor(red, green, blue)
    
    # main program
    turtle.title("My Turtle")
    turtle.setup(500,500, 0,0)
    
    # bind keys to our functions
    turtle.onkey(go_up, "Up")
    turtle.onkey(go_down, "Down")
    turtle.onkey(go_left, "Left")
    turtle.onkey(go_right, "Right")
    turtle.onkey(toggle_pen, "p")
    turtle.onkey(draw_poly, "q")
    
    # listen for key presses
    turtle.listen()
    
    turtle.exitonclick()
  9. You can ask the user for input in Turtle Graphics mode in a manner similar to the way you would do so using standard text-based IDLE mode using the turtle.textinput() function. This function takes two arguments – a window title and a prompt. It returns a string just like the regular input() function, and it pauses execution until the user types in a value and clicks OK. Here’s an example that continually prompts a user to enter in a number of sides for a polygon and then draws the requested shape. Note that this example also uses the turtle.clear() function which clears the canvas when called.
    import turtle
    import random
    
    def polygon(sides, length):
    
        for x in range(sides):
            turtle.forward(length)
            turtle.right(360/sides)
    
    turtle.title("My Turtle")
    turtle.setup(500,500, 0,0)
    
    keepgoing = True
    
    while keepgoing == True:
    
        # get a number of sides from the user
        sides = int(turtle.textinput("Title Window", "Enter # of sides"))
    
        # if the user enters a 0 then we can end the loop
        if sides == 0:
            keepgoing = False
        else:
            # clear the canvas
            turtle.clear()
    
            # draw the polygon
            polygon(sides, 100)
    
    turtle.exitonclick()
  10. We also built a few demos in class that showcased some of these techniques. Here’s a run-down of the code that we wrote:
    Simple Graphing Application

    import turtle
    import random
    
    # input:  takes a side and a length as integers
    # processing:  draws a figure based on the inputs
    # output:  nothing, dones't return any value
    def polygon(sides, length):
    
        for x in range(sides):
    
            turtle.forward(length)
            turtle.right(360/sides)
    
    turtle.title("My Turtle")
    turtle.setup(500, 500, 0, 0)
    
    # start off on the left side of the screen
    xpos = -200
    
    # get 7 days worth of weight values from the user
    for days in range(7):
    
        # get this days weight
        weight = float(turtle.textinput("Weight", "Weight for today: "))
    
        # draw a rectangle on the screen
        turtle.penup()
        turtle.goto(xpos, 0)
        turtle.setheading(90)
        turtle.pendown()
        turtle.forward(weight)
        turtle.right(90)
        turtle.forward(50)
        turtle.right(90)
        turtle.forward(weight)
        turtle.right(90)
        turtle.forward(50)
    
        # move to the right
        xpos += 60
    
    turtle.exitonclick()

    Nested Boxes

    import turtle
    import random
    
    # input:  takes a side and a length as integers
    # processing:  draws a figure based on the inputs
    # output:  nothing, dones't return any value
    def polygon(sides, length):
    
        for x in range(sides):
    
            turtle.forward(length)
            turtle.right(360/sides)
    
    turtle.title("My Turtle")
    turtle.setup(500, 500, 0, 0)
    
    # start off with a square of 50 pixels
    size = 200
    
    # start off at -150, 150
    xpos = -150
    ypos = 150
    
    # start off with a very red color
    green = 1.0
    
    turtle.tracer(0)
    
    for x in range(10):
    
        # move to our desired location
        turtle.penup()
        turtle.goto(xpos, ypos)
        turtle.pendown()
    
        # draw our polygon
        turtle.pencolor(0,green,0)
        turtle.fillcolor(0, green, 0)
        turtle.begin_fill()
        polygon(4, size)
        turtle.end_fill()
    
        # change pos
        xpos += 10
        ypos -= 10
    
        # decrease green
        green -= 0.1
    
        # decrease the size
        size -= 20
    
    turtle.update()
    
    turtle.exitonclick()

    Dynamic Skyline!

    Screen Shot 2013-11-12 at 3.04.03 PM

    import turtle
    import random
    
    # function:    block
    # input:       x & y position (integers), color (integer) and window flag (boolean)
    # processing:  creates a block at the specified location with the specified color
    #              and the windows (if set)
    # output:      returns nothing
    def block(x, y, color, windows):
    
        # pick up the pen and move to the spot in question
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
    
        # face right to ensure we are drawing from the correct starting point
        turtle.setheading(0)
    
        # fill using the color - this will create a greyscale color
        turtle.fillcolor(color, color, color)
        turtle.begin_fill()
    
        # draw a box
        for i in range(4):
            turtle.forward(50)
            turtle.right(90)
        turtle.end_fill()
    
        # if we want to draw windows we can do so here
        if windows:
            turtle.fillcolor(1,1,1)
            turtle.penup()
            turtle.goto(x+10, y-10)
            turtle.pendown()
    
            print(x+10, y-10)
    
            turtle.begin_fill()
            for i in range(2):
                turtle.forward(30)
                turtle.right(90)
                turtle.forward(10)
                turtle.right(90)
            turtle.end_fill()
    
    # now draw a random skyline!
    
    # where to start?
    xpos = -200;
    ypos = -200;
    
    # how big is each blocK/
    blocksize = 50
    
    # how many buildings?
    for building in range(5):
    
        # how many stories in this building?
        stories = random.randint(1, 10)
    
        # iterate over this range and draw our building here
        for story in range(stories):
    
            # should this block have windows?
            if (random.randint(1,2) == 1):
                windows = True
            else:
                windows = False
    
            # draw a block here
            block(xpos, ypos, random.random(), windows)
    
            # update ypos so we can go up for the next story
            ypos += blocksize
    
        # end of the loop!  reset ypos
        ypos = -200
    
        # also move our xpos over so we can draw the next building
        xpos += blocksize