Lab - Loops - Magic 8 Ball
This lab explores using loops with user input, specifically while loops. Looping until a user indicates they want to stop, or looping until a user enters a valid input.
Acknowledgements¶
The code for this lab was originally developed by Arch Harris and Nancy Harris.
Objectives¶
- Use a loop structure for error handling
- Use a loop structure for execution control
Background¶
The Magic 8-Ball is a toy produced by Tyco Toys (now Mattel) and consists of a ball filled with a blue fluid. Suspended in the fluid is a icosahedron (a 20-sided polyhedron with each side consisting of an equilateral triangle). Each face has an answer to a yes/no question.
Starting Code¶
You are given the produce_response
function which produces a response at random, from a set of 20 possible responses.
import random
def produce_response():
# pick a random answer
answer_index = random.randint(1, 20)
answers = ["", "Signs point to yes.", "Yes.", "Reply hazy, try again.",
"Without a doubt.", "My sources say no.",
"As I see it, yes.", "You may rely on it.",
"Concentrate and ask again.",
"Outlook not so good.", "It is decidedly so.",
"Better not tell you now.", "Very doubtful.",
"Yes - definitely.", "It is certain.",
"Cannot predict now.", "Most likely.",
"Ask again later.", "My reply is no.",
"Outlook good.", "Don't count on it.", "HUH?"]
return answers[answer_index]
STEP 1¶
Complete the input_yes_no
function described in the docstring below.
The function displays the given prompt
string followed by the string "[yes/no]: "
, reads the next line of user input, and returns True
if the user has entered y
or yes
, ignoring case. For any other input, it returns False
.
def input_yes_no(prompt):
"""Displays the given prompt followed by [yes/no].
Returns true if the next line of user input is "y" or "yes" ignoring case,
false otherwise. In order to convert what the user has entered to either
all caps or all lowercase you can use:
upper_string = user_string.upper()
lower_string = user_string.lower()
Args:
prompt(string): the text to display to the user
Returns:
bool: return True if user entered 'y' or 'yes', or False if not
"""
STEP 2¶
Complete the get_question
function described below. This function should keep prompting the user (using the given prompt
string) to enter a question until they enter a valid one. Question strings must be between 1 and 60 characters, and they must end with a question mark. If the question is invalid, display the applicable error message:
- "Your question is blank"
- "Your question is too long"
- "Your question must end with a ?"
def get_question(prompt):
"""Gets the next question from the user.
The length of the question must be between 1 and 60 characters, and the
question must end with a '?'. This function keeps asking the user for a
question until they enter a valid one.
Args:
prompt(string): the text to display
Returns:
(string): the user's question
"""
STEP 3¶
Complete the main
function below. In this function you should keep asking the user if they want to ask a question until they do not respond yes. You should create a loop using the input_yes_no
function given above to accomplish this. Each time the user agrees to ask a question, use the get_question
function given above to have the user enter a valid question. Once the user has entered a valid question, then use the produce_response
function given above to produce a random response and print out the result.
Look at what's being printed to decide what to write on the missing lines.
def main():
print("Magic 8 Ball\n")
# Run the program as long as the user wants
# FILL IN THE MISSING CODE IN THE WHILE BELOW - use your function
# to set up this while loop
while ( ):
# get the next question
# FILL IN THE MISSING CODE IN THE LINE BELOW - use your function
# to get a valid question
question =
print()
# get an answer
# FILL IN THE MISSING CODE IN THE LINE BELOW - use the function
# to get a random answer
answer_str =
# output the results
print(f"Question: {question}\n")
print(f" Answer: {answer_str}\n")
print("Goodbye!")
STEP 4¶
Run the program with various inputs to test it. Here is a sample interaction:
Magic 8-Ball
Do you want to ask a question? [yes/no]: yes
What is your question? Will I win the lottery
Your question must end with a ?
What is your question? Will I win the lottery?
Question: Will I win the lottery?
Answer: Signs point to yes.
Do you want to ask a question? [yes/no]: no
Goodbye!