Lesson 5: Use functions to program a game

Instructions: For this class, you are going to program a simple number guessing game! The user will guess a number, and the computer will generate a random number. If this two numbers match, the user gains points. If they're different, the user loses points. Write the following functions in python, using what you've learned so far. Make sure to test your code, and use good python style.

Tip: If you want to clear the output from a cell, you go to the "Cell" menu above, then "Current Output", then "clear". If you started a game, but don't want to finish it, you can stop it by going to "Kernel", then "Interrupt".

1. Ask the user for their guess.

Write a function that asks the user for a number guess between 1 and 10, inclusive. This function should be very few lines of code. Call this function ask_input.

Hint: use the command raw_input() in your function to ask for and take in a variable from the user.


In [2]:

2. Generate a random number, and check if it matches the guess.

Write a function that takes in an int, which will correspond to the user's guess, generates a random number, and checks if it is equal to the guess. This function should return a boolean, whether or not the guess was correct. It should also print out a message, telling the user if their guess was correct or not. If their guess was wrong, it should tell them what the correct number was. Call this function findNumMatchStatus. Also, remember to import the random module at some point before you run the code!

Hint: Make sure to convert the string returned by ask_input into an integer using int() before you checking for equality. In python, the string "5" is not equal to the integer 5.


In [0]:

3. Write code that uses the functions you wrote, in addition to new code, to carry out one round of game play.

The user should be prompted for their guess, using the above function, and that guess should be checked, using another function above. If the user's guess is correct, this function should return 10. If it is incorrect, return -1. Call this function play_round.


In [4]:

4. Now play a few rounds of the game. You should be able to play a round by running the command play_round().


In [0]:
play_round()

Homework exercise (10 Points)

For this homework, you will be taking the function you wrote in class, and extending them. You may find yourself wanting to change the functions written above to make them more general. It's fine to modify them, as long as they still complete the basic task above.

1. Now, make the game multi-round, by calling the play_round function in a loop. Give the user 5 points to start the game. Each round, tell the user their score (by printing it). The game ends when either the user gets to 0 points (in which case they lose) or they reach 25 points (in which case they win). Tell the user if they won or lost using a print statement. When the game ends, the game should stop. This does not need to be a function.

Hint: you can use a while loop to perform a function an infinite number of times until its condition is met.


In [0]:

2. Now, we are going to make a slightly different version of the game. This time, instead of trying to reach 25 points in an undetermined number of rounds, the user will be trying to get as many points as possible in 10 guesses. The user should start with 0 points, and be asked for a number guess 10 times. After each guess, they should be told whether their guess was correct or not, and the correct number, like above. Scoring is the same as above. Therefore, the maximum score is 100, and the minimum is -10. They should be told their final score, which will be the sum of their individual scores from each round.

Hint: you can use a for loop to run a command a set number of times.


In [0]:

Keep pythoning!!

If you're enjoying working with Python, these materials are a small portion of materials developed by GCB student Sarah Middleton. The complete collection can be found here.