CS21 Lab6: While Loops and Functions

Due 11:59pm Thursday night, Oct. 23

Run update21, if you haven't already, to create the cs21/labs/06. Then cd into your cs21/labs/06 directory and create the python programs for lab 3 in this directory (handin21 looks for your lab 6 assignments in your cs21/labs/06 directory):

$ cd cs21/labs/06
$ pwd
  /home/your_user_name/cs21/labs/06

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

1. GetLetter function
Write a program, getletter.py, that calls a function getLetter. Your getLetter function should prompt the user to enter an alphabetic character (a-z, and A-Z) and return the letter to the caller. getLetter should continue to prompt the user to enter a letter until s/he enters a valid alphabetic character. Your program should call getLetter and then print out the value it returns.

Here are a few example runs of a working program:


$ python getletter.py

Enter a letter: 8
Hey, '8' isn't an alphabetic character, try again...
Enter a letter: ^
Hey, '^' isn't an alphabetic character, try again...
Enter a letter: abcdef
Hey, 'abcdef' isn't an alphabetic character, try again...
Enter a letter: A
The letter entered is: A


$ python getletter.py

Enter a letter: h
The letter entered is: h

2. Guess A Letter

Copy your getletter.py file to game.py:

cp getletter.py game.py
Modify the program in game.py to implement a program that will pick a random alphabetic character, and the prompt the user to try to guess which letter was selected. You should add at least one function that takes as input a list representing the letters already guessed, calls your getLetter function from problem 1 to get a letter input value from the user, and if the user has not already guessed this letter returns it to the caller, otherwise the function keeps repeating these actions until the user enters a letter s/he has not yet guessed. Your main function should call this new function repeatedly until the user guesses the correct letter. Your program should print out a message after each turn, indicating if the user's guess is correct or if s/he needs to guess again.

To pick a random value, you need to first import functions from the random library:

from random import *
Then you can call the randrange to get a random value from the specified range. For example, to pick a random value from the range 0,26 you would make the following call:
rand_val = randrange(0,26)

Here is what a few runs of your program might look like:


$ python game.py

I'm thinking of a character between 'a' and 'z'
Let's see if you can guess it

Enter a letter: a
'a' is incorrect, try again...
Enter a letter: b
'b' is incorrect, try again...
Enter a letter: 9
Hey, '9' isn't an alphabetic character, try again...
Enter a letter: B
You already guessed 'b', try again...
Enter a letter: c
'c' is incorrect, try again...
Enter a letter: d
'd' is incorrect, try again...
Enter a letter: e
'e' is correct, you win in 5 guesses!!!

$ python game.py

I'm thinking of a character between 'a' and 'z'
Let's see if you can guess it

I'm thinking of a character between 'a' and 'z'
Let's see if you can guess it
Enter a letter: A
'a' is incorrect, try again...
Enter a letter: a
You already guessed 'a', try again...
Enter a letter: b
'b' is incorrect, try again...
Enter a letter: C
'c' is incorrect, try again...
Enter a letter: d
'd' is incorrect, try again...
Enter a letter: E
'e' is incorrect, try again...
Enter a letter: F
'f' is incorrect, try again...
Enter a letter: sdflsls
Hey, 'sdflsls' isn't an alphabetic character, try again...
Enter a letter: 9
Hey, '9' isn't an alphabetic character, try again...
Enter a letter: g
'g' is correct, you win in 7 guesses!!!



Submit
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.