Lab: Functions
In this lab, you will practice writing functions. You can freely choose the exact words to be printed by your functions, because you are submitting into Canvas, not gradescope.
You will develop a set of functions to power a number guessing game. Once your functions are developed and tested, you will be able to run the full game.
STEP 1 - a function with no parameters and no return values¶
The first function you will develop is named show_welcome
. It takes no parameters and returns nothing.
It should print a message to welcome the player to your game - any message you want.
Write the function and test the function by calling it in a main block at the bottom of your file.
if __name__ == "__main__":
STEP 2 - a function with no parameters and a return value¶
The second function to develop is named select_level
. It takes no parameters and it returns an int
value.
The function should ask the user what level game they want to play (easy, medium, or hard). The message should tell the user what levels they can choose from, for example, something like:
"What level do you want to play (E,M,H)? “
You can choose the words (and letters) you want. However, if the user chooses easy (‘E’), the function should return 10, for medium, return 100, and for hard, return 1000. Those numbers correspond to the range of numbers in the guessing game: 1 to 10, 1 to 100 or 1 to 1000. If the user types in anything other than the 3 choices you’ve given them, then your function should return 10.
Write the function in the first cell below, and test the function by calling it in the main block at the bottom of your file.
STEP 3 - a function with parameters and a return value¶
The third function to develop is named check_guess
. This function takes 2 parameters and returns a bool
value. The parameters are both int
values. The first parameter represents the player’s guess, and the second one represents the secret number. This function will compare the player’s guess to the secret number. If the guess is bigger than the secret number, print a message that it is too high and return False
. If the guess is less than the secret number, print a message that it is too low and return False
. However, if the guess is the same as the secret number, print that it is correct and return True
.
Write the function in the first cell below, and test the function by calling it in the main block. Write at least 3 calls to this function to test the 3 cases (too high, too low and correct), and make sure the right messages are printed and the right value is returned.
STEP 4 - run the game program¶
Copy and paste the following function play_game
into your file just below the 3 functions you have written. You can move the import
statement to the top of the file.
import random
def play_game( max ):
tries = 0
secret = random.randint(1, max)
print( "I'm thinking of a number between 1 and " + str(max) + "..." )
while (guess := input( "Guess or type q to give up: " )) != 'q':
guess = int(guess)
tries += 1
if check_guess(guess, secret):
print( "It only took you " + str(tries) + " tries.\n")
break
else:
print( "You quit after only " + str(tries) + " tries?? That's weak.\n")
Copy and paste the following code into your main block at the bottom of the file.
# Play the guessing game
show_welcome()
while True:
max = select_level()
play_game(max)
answer = input( "Do you want to play again? Enter 'y' or 'n': ")
if answer == 'n':
break
When you run your program, it should play a number guessing game, using the messages that you wrote in your functions. The code that implements the game uses loops, which we haven't studied yet, so don't worry if you can't understand it all right now.