CSE 110: Introduction to Programming

W04 Code-along Activity: Guess My Number Game

Code-along Activity Overview

For this activity, you will code along with an instructor video that walks you through each step of the activity below. Make sure that you pause the video, or watch sections over if you need to, so that you are able to complete the entire activity.

Your instructor will post a link to the video in Microsoft Teams.

Instructions

Create a game where the computer selects a random number and the user tries to guess it. After each guess, the computer tells the user if they should guess higher or lower.

The game continues until the user has guessed the random number. Then, the user is told how many guesses they made

Enhancement

Once the basic game is working, add the ability for the user to play again and start the whole process over again.

A sample run of the program could look as follows:


What is your guess? 35
Higher
What is your guess? 65
Higher
What is your guess? 80
Higher
What is your guess? 90
Lower
What is your guess? 85
Lower
What is your guess? 83
You guessed it!
It took you 6 guesses

Would you like to play again (yes/no)? yes
What is your guess? 83
Lower
What is your guess? 40
Lower
What is your guess? 20
Higher
What is your guess? 30
Higher
What is your guess? 37
Lower
What is your guess? 33
Higher
What is your guess? 35
Higher
What is your guess? 36
You guessed it!
It took you 8 guesses

Would you like to play again (yes/no)? no
Thank you for playing. Goodbye.

Background Information

This assignment is a little tricky, because it brings together many of the concepts you've learned in this course including loops and if statements.

Having the computer pick a random number

There is a random number library included with Python that contains a number of different functions to generate random numbers, depending on if you want integers, floating point numbers, and from different statistical distributions.

The only function you will need from this library is called randint. To use it, you give it two numbers and it returns back a random number in that range. In order to use this function you need to import it from the random library.

The following code shows how to import the function and use it to get a random number from 1 to 10.


import random

number = random.randint(1, 10)
print(number)
Importing Libraries

When importing code from another library, you only need to include the import statement once in your program, and it is considered good practice to put all of your import statements together at the top of the program. That way, it is easy for others that you work with to see the libraries you are using.


import random

# Lots of other code here...
# code
# code
# more code
# ...

# Then you can call the library function wherever you need it.
number = random.randint(1, 10)
print(number)

Assignment

Find the video that your instructor has posted for this assignment and code along with them for each of the following steps.

Please work through the requirements in order with the instructor rather than jumping ahead to more complicated steps, to ensure that you are understanding the fundamental concepts.

Sample Solution

In addition to the instructor video, you can also view this sample solution. (This solution may be slightly different than the final code that you and your instructor created.)

Alternative Video Link

In addition to the link that your instructor posted, you are also welcome to watch the following code walkthrough or use its transcript:

Submission

Please pause and re-watch any sections of the instructor video necessary until you have completed the entire activity and your program runs the same as the instructor's. When you are finished:

Up Next

Other Links: