In Class: Week 3


Create a week03 subdirectory in your cs21/class directory:

      % cd 
      % cd cs21/class
      % pwd
        /home/your_user_name/cs21/class

      % mkdir week03        
	
Then, from within your week03 subdirectory copy over some python files from my public directory:
    % cd week03
    % pwd
      /home/your_user_name/cs21/class/week03

    % cp ~newhall/public/cs21/week03/* .
    % ls
      ascii.py   drop_letter.py   format.py   is_odd.py   what_is_it.py
	

We are going to do some of the following this week:
  1. Together let's write a program, charvalue.py, that asks the user to enter an int and then print out the ascii character encoded with the int value. So if the user enters the value 66, our program will print out the character B.

    Once we have tested this, let's see what happens if the user enters 10000. How can we fix this?

  2. We are going to modify the program above slightly, so first lets copy it to a new file, alphavalue.py, and we will open this in vim and modify it to solve the slightly different problem:
    	$ cp charvalue.py alphavalue.py
    	$ vim alphavalue.py
    	
    Let's change alphavalue.py so that in interprets input values differently than charvalue.py. Instead of 1 corresponding to the character encoded with ascii value 1, we will interpret 1 to correspond to the first character in the alphabet 'a', 2 to 'b', and 26 to 'z'. Our program will only encode letters between a and z. How can we change our program to do this? What about input values that are larger than 26 (again, we will ignore the case of bad input values that are less than 1 for now)? How can we handle these?

  3. open ascii.py. Together we are going to write a program that takes a line of input from the user, and prints out the ascii values of each character in the input string. Here is what a run of your program might look like:
    $ python ascii.py
    
    Enter a phrase: hello there
    
    h : 104
    e : 101
    l : 108
    l : 108
    o : 111
      : 32
    t : 116
    h : 104
    e : 101
    r : 114
    e : 101
    

  4. open drop_letter.py. Together we are going to write a program that takes a line of input from the user, and creates a new string that consists of every other character (every even character) from the input string (we will say that zero is even). Here is what a run of your program might look like:
    This program takes as input a string and produces a new
    string cosisting of every other (even) character from the first
    
    Enter a phrase: I love computer science! 
    The modified string is: Ilv optrsine
    

  5. open what_is_it.py. Together we are going to complete the program that asks the user to enter a character from the keyboard and then tells the user what type of character it is (lower case, upper case, digit, or other).

    Once you get that working add code to check that the user correctly entered just a single character. If not, then print out an error message saying that the input is bad and quit the program by making a call to the exit function:

    print "bad input"  # come up with a better message than this
    exit()
    

  6. open is_odd.py. Write a program that asks the user to enter an integer value and then prints out a message indicating if the value is odd or not.

    Once you get that to work, lets add code to first check to see if the input value entered by the user is a valid int (e.g. the string "hello" is not a valid int value). If the input is not valid, print out an error message and exits. If it is valid, then print out a message indicating if it is an odd or even number.

  7. open format.py in vim. It contains some examples of formated output. Try running it in another terminal window and see if you understand what is getting printed out.