CS21 Lab 6: While Loops and Functions

Due 11:59pm Tuesday, March 2

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

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page. (You will be using this code again in Lab 07 so please make sure it is well-written and well-commented!)

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. The getLetter function should continue to prompt the user to enter a letter until they enter a valid alphabetic character. The getLetter function should only print error messages and not the final accepted letter. The main function should call getLetter and then print out the letter getLetter 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

Guess A Letter

Copy your getletter.py file to guessLetter.py:

cp getLetter.py guessLetter.py
Modify the program in guessLetter.py to implement a simple guessing game where the computer picks a random alphabetic character and the user tries to guess which letter was selected.

You should add a function getNewLetter to your game that:

  1. Takes as input a list representing the letters already guessed.
  2. Calls your getLetter function from problem 1 to get a letter from the user -- this function should be identical to the function used in getLetter.py.
  3. If the letter guessed is not one the user has already guessed, update the list of guessed letters indicating that this newly guessed letter has been guessed, and return the letter to the caller
  4. If the letter is one that has already been guessed, then repeat step (2) until the user enters a letter that has not already been guessed.
Add some calls from main to test that your getNewLetter function is working correctly.

Once you are sure that it works, modify your main function so that it implements a letter guessing game. Your program should pick a random letter and then make calls to your getNewLetter function until the user correctly guess the randomly selected letter. Your program should print out a message after each turn, indicating if the user's guess is correct or if they need to guess again. When the player wins, your program should print out the number of guesses that the user made before winning.

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

from random import *
Then you can call the randrange function 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 guessLetter.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 guessLetter.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: 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.