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.
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 game.py:
cp getletter.py game.pyModify 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!!!