CHALLENGE EXERCISE #1
- Define and call a main function.
- Add a welcome message to the user.
- Ask the user to enter a yes or no question and take in their input.
- Define a function askthat generates a random number and responds
- Ask the user if they would like to ask another question.
- Loop the program based on the user's answer.
TASK:
Define a function called check_input
that takes in the question from the user and error checks the question. This function should only return the question to main if the user typed in a valid question.
HINT:
You will need to start by using raw_input to receive the question from the user. Then, your program will need a branch that will check if the question has a '?' or if its an empty string. You can add additional checks if you'd like before returning the question to main.
CHECK UP:
Watch the video below to check that you completed the task.
TASK:
Generalize the function check_input
so that it will error check for both the question and whether or not the user would like to run the program again.
HINT:
You will need to define check_input
with a parameter that will be used in a branch. The first branch will check if the question is valid, while the second branch will check if the user entered yes or no.
CHECK UP:
Watch the video below to check that you completed the task.
TASK:
Cause check_input
to repeatedly take in input from the user if what they entered doesn't pass the error checking algorithm.
HINT:
To add this functionality, you will need to have an infinite while loop with the call to raw_input in the body. Your algorithm should only return a value if the input has passed all the checks. If not, the while loop should continue iterating.
CHECK UP:
Watch the video below to check that you completed the task.
TASK:
Add a function called ai
and set the return value of check_input as its argument.
HINT:
.
CHECK UP:
Watch the video below to check that you completed the task.
TASK:
Define a function get_a
such that it contains a branch looking for keywords that will anticipate the user’s question and runs rand_grade.
CHECK UP:
Watch the video below to check that you completed the task.
TASK:
Define rand_grade
such that it takes in a parameter specifying which type of question and prints out an intelligent response.
HINT:
The red button at the top of the screen can be fixed by adding the following code you your while loop.
for event in pygame.event.get():
If event.type == pygame.QUIT:
sys.exit()
You also need to add If event.type == pygame.QUIT:
sys.exit()
“import sys”
at the top of your program.
CHECK UP:
Watch the video below to check that you completed the task.
CHALLENGE EXERCISE #2
- Add checks for 3 more types of questions related to grades to function 'get_a'.
- Add another category of questions to your Magic 8-Ball such that function 'ai' now has two function calls in its body.
TASK:
Spell check the input using the Enchant module.
HINT:
You will need to import the Enchant module at the top of your program. Then, you will need to run a function on the user’s question. This function will check each word’s spelling using the set_text method.
CHECK UP:
Watch the video below to check that you completed the task.
CHALLENGE EXERCISE #3
- Make your program respond with 3 different random responses to a mispelled word in the question.
TASK:
Use an array that will keep track of how many times each question was asked.
HINT:
You will need to define an array that has as many elements as questions you are checking for. Define the array in the main function and initialize the array elements to zero. You will then need to pass the array to the function calls so that it can later be correctly incremented.
CHECK UP:
Watch the video below to check that you completed the task.
TASK:
Define a boolean function called check_repeat that will take in the array and question index as arguments and increment the array at the provided index.
HINT:
You will need to define the function check_repeat so that it checks if the array element at the index is greater than 0, which means the first type of question was asked previously. If this is true, the function should return True, otherwise return False.
CHECK UP:
Watch the video below to check that you completed the task.
CHALLENGE EXERCISE #4
- Define a function called print_repeat that will respond using 3 different responses for the same question.
- Run print_repeat if check_repeat returns True.
TASK:
Complete the Magic 8-Ball project.
HINT:
The key to this task is to think of the screen like a painting. If you add paint, then there is just more paint on the painting, the old paint doesn’t go away. You have to paint over something you don’t want anymore to get rid of it.
CHECK UP:
Watch the video below to check that you completed the task.
Congratulations! You have completed Magic 8-Ball!