CS21 Lab6: While Loops and Functions

Due 11:59pm Thursday night, Oct. 22 (note the due date change)

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 6 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.

This week you are going to implement and test some functions that you will use in lab 7. Incrementally implementing and testing functions is an important way to approach implementing larger programs. It will save you hours/days of debugging time to implement and test one function at a time, so this week we are giving you some practice on this skill.

Because of fall break, we are giving you until the Thursday evening after fall break to submit this assignment. However, we strongly encourage you to get it done before it is due (try for Tuesday evening).

1. GetMenuOption function
In a file named getmenuoption.py:
  1. write a function called, PrintMenu, that when called prints out the following menu of options:
    What do you want to do next?
     1. Spin the Wheel
     2. Buy a Vowel
     3. Guess the Puzzle
     4. Quit, I can't stand this game anymore!
    

    Make a call to PrintMenu from main to test it out. Once you are sure it works, remove the call to PrintMenu from main (this was just for testing purposes).

  2. Next, write another function, GetMenuOption, that calls PrintMenu to print out the menu of options, prompts the user to enter his/her choice and returns the user's menu choice (a value between 1 and 4). Your GetMenuOption function should detect bad user input, print out an appropriate error message when the user enters bad input, and re-prompt the user to try again. GetMenuOption should only return a value between 1 and 4.

    In main, make a call to GetMenuOption and print out the value it returns:

Here are some sample runs of a complete program:
$  python getmenuoption.py

What do you want to do next?
 1. Spin the Wheel
 2. Buy a Vowel
 3. Guess the Puzzle
 4. Quit, I can't stand this game anymore!

Enter your choice: 1

GetMenuOption returned: 1

$  python getmenuoption.py

What do you want to do next?
 1. Spin the Wheel
 2. Buy a Vowel
 3. Guess the Puzzle
 4. Quit, I can't stand this game anymore!

Enter your choice: hello
Hey, hello isn't a valid option.  Try again...
Enter your choice: a
Hey, a isn't a valid option.  Try again...
Enter your choice: -3
Hey, -3 isn't a valid option.  Try again...
Enter your choice: 5
Hey, 5 isn't a valid option.  Try again...
Enter your choice: 2

GetMenuOption returned: 2
2. IsAVowel and GetAConsonant functions
In a file named, guessconsonant.py:
  1. Write a function, IsAVowel, that takes a single character as input input (i.e. a string of length 1) and returns True if the character is a vowel (a, e, i, o, or u) and False otherwise.

    Add some calls from main to your IsAVowel function printing out its return value. Test cases where you expect it to return True and cases where you expect it to return False.

    Once this works, remove these calls from main (these were just for testing purposes).

  2. Next add a function called GetAConsonant that gets a consonant from the user and returns it to the calling program. Your function should have one parameter: a 26-element list of bool values, used_letters, representing which letters have been guessed already and which have not (entry 0 corresponds to 'a', entry 1 to 'b', and so on).

    Your function should prompt the user to enter a consonant, it should check to see if the user's input is valid, and if so, it should set the corresponding used_letters entry to True (the user has now used this letter), and return the user's guessed letter (a single char string) to the caller.

    If the user's input is not valid, your function should print out an error message describing in what way it is not valid, and prompt the user to try entering a consonant again. When you are testing for valid input, make a call to your IsAVowel function when appropriate.

    Here are some ways in which a user's input is invalid:

    1. The guess is a string of more than one character
    2. The guess is not an alphabetic character
    3. The guess is a vowel
    4. The guess is a consonant that has already been guessed

    Your function should only return a value after the user enters a valid consonant.

    To test your function, add a couple calls from your main function to GetAConsonant and print out its return values. To call this function, you need to create the list of 26 bool values that you pass to it. Once you are satisfied that GetAConsonant is correct, remove these calls from main (these were just for testing).

  3. Next, we are going to implement a game program in main that makes calls to your GetAConsonant function. This is a game called Guess a Consonant in 5 guesses or less. Your program will choose a secret letter (just always have it "choose" the letter 'X' to make testing your program easier), and then at each turn:
    1. call GetAConsonant to get the next consonant guess from the user
    2. check to see if the user guessed correctly (did they guess 'X'?)
    3. if so, print out a message that they won the game in how many number of guesses it took them.
    4. if not, then if they have guesses left goto step (1), otherwise, they lost the game (print out a message that they lost and what the secret letter was

Here are a few example runs of a working program:

$ python guessconsonant.py

Try to guess the consonant I'm thinking of in 5 guesses:

 Enter a consonant guess: t

Nope, T is not right, you have 4 guesses left.

 Enter a consonant guess: T
 Hey, you already guessed T Try again...
 Enter a consonant guess: t
 Hey, you already guessed T Try again...
 Enter a consonant guess: a
 Hey, A is a vowel. Try again...
 Enter a consonant guess: hello
 Hey, hello isn't a single letter. Try again...
 Enter a consonant guess: xxx
 Hey, xxx isn't a single letter. Try again...
 Enter a consonant guess: 9
 Hey, 9 isn't a letter. Try again...
 Enter a consonant guess: r

Nope, R is not right, you have 3 guesses left.

 Enter a consonant guess: x

You correctly guessed X in  3 tries!

$ python guessconsonant.py

Try to guess the consonant I'm thinking of in 5 guesses:

 Enter a consonant guess: t

Nope, T is not right, you have 4 guesses left.

 Enter a consonant guess: r

Nope, R is not right, you have 3 guesses left.

 Enter a consonant guess: s

Nope, S is not right, you have 2 guesses left.

 Enter a consonant guess: p

Nope, P is not right, you have 1 guesses left.

 Enter a consonant guess: q

Nope, Q is not right, you have 0 guesses left.

You did not guess X better luck next time.


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.