Computer Science 111, Spring 2012

Lab #3

February 15 and 16, 2012

The purpose of today's lab is to work with data structures (strings and lists) and sequence controllers (definite loops and the decision structure if-statement).
Work with a partner as before.

python3 statements/functions used:

  1.  a definite loop (for loop)
      
      for i in list:
          <statements>
      We will use a list written explicitly with square brackets,
         or a list that is stored in a variable.
  2.  string functions.
      examples 
      x = "Computer Science is great"
      words = x.split()  makes a list ["Computer","Science","is","great"]
       We can use this list in a definite loop 
          for i in words:
              print(i)
  3.  random numbers
      x = random.randrange(4,9)
      x will have a value that is either 4,5,6,7, or 8
  4.  equality operator ==
          "brown" == "brown" is True
          "brown" == "white" is False
  5.  if-statement
        if <True or False condition>:
	     <statements>
      if word=="You":
          print("Hey you!")

Get a copy of the file lab3judy.pyc from my handout directory (with the command getcopy lab3judy.pyc)
- and then run this program by invoking the python interpreter, and typing import lab3judy. The program will run right away because I placed a call to main() at the end of the file lab3judy.py.
Note that you only get to have the .pyc file, which has already been interpreted, and is not the source file, full of python statements that would tell you how to write the program!

This is the program that you will write for the this lab

Use emacs to write your own version of the program. This is not a deep, meaningful program. It's just a way to try out some of python's functions and statements.
I have broken it into three parts. Get each part to work first before moving on the the next part.
Here is what your program should do:

Part 1.

Part 2.

Part 3. Now try out the if statment

Now use the input() function to ask the user to say something with lots of "Is" and "Yous" in it. Assign the user string to the varialbe message.

Type the following statements at the end of your lab3.py main() function definition:
    message = message.split()
    computer = ""
    for word in message:
        old_word = word
        word = word.replace("I","You")
        if old_word==word:
            word = word.replace("You", "I")
        computer = computer + word + " " 
    print(computer)
This code takes each word from the sequence message, and first places the word in a variable called old_word, for safe-keeping.
The statement
        word = word.replace("I","You")
finds all occurrences of the string "I" in word, and replaces it with "You". The variable word will only contain one word so it is either replaced or not. It then assigns the resulting string to the variable word. The next line is the beginning of an if statement
        if old_word==word:
it checks if old_word and word are the same using the equality operator (==). This is a nice feature of python. If they are the same, then no replacement happened. In that case, we want to try
            word = word.replace("You", "I")
and if the word contains "You", we'll replace it with "I" and assign the new string to word.
  • After this statement, but still inside the for loop, concatenate the string in variable word to variable computer, with the string that contains one space, " ", as before. After the loop statement, print out the string contained in the variable computer.

    Make sure all three parts are in just one function definition, for function main(). And make sure there is one call to main() at the end of your file, without indentation. All three of these parts are useful for the homework assignment.