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):
$ update21 $ cd cs21/labs/06 $ vi getletter.py
Your programs are graded on both correctness and style. Please review
the comments regarding programming style on the main page.
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. 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
Copy your getletter.py file to guess.py:
cp getletter.py guess.py
Modify the program in guess.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 to your game that:
Your main function should call this new function repeatedly until the user wins the game by guessing the correct 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.
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 guess.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 guess.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!!!
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.