0% found this document useful (0 votes)
166 views

09-Guessing Game Challenge

The document outlines a guessing game challenge where a program picks a random number between 1 and 100 and has players guess the number. The rules are that if a guess is out of bounds, it returns "OUT OF BOUNDS", and on the first guess returns "WARM!" or "COLD!" depending on how close it is to the number. On subsequent guesses it returns "WARMER!" if closer or "COLDER!" if farther. When the player guesses correctly it tells them how many guesses it took. It provides hints to use random integers, store guesses in a list, and use while loops to continually ask for guesses and check if they are correct.

Uploaded by

mayur jabare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views

09-Guessing Game Challenge

The document outlines a guessing game challenge where a program picks a random number between 1 and 100 and has players guess the number. The rules are that if a guess is out of bounds, it returns "OUT OF BOUNDS", and on the first guess returns "WARM!" or "COLD!" depending on how close it is to the number. On subsequent guesses it returns "WARMER!" if closer or "COLDER!" if farther. When the player guesses correctly it tells them how many guesses it took. It provides hints to use random integers, store guesses in a list, and use while loops to continually ask for guesses and check if they are correct.

Uploaded by

mayur jabare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6/15/2019 09-Guessing Game Challenge

Guessing Game Challenge


Let's use while loops to create a guessing game.

The Challenge:

Write a program that picks a random integer from 1 to 100, and has players guess the number.
The rules are:

1. If a player's guess is less than 1 or greater than 100, say "OUT OF BOUNDS"
2. On a player's first turn, if their guess is
within 10 of the number, return "WARM!"
further than 10 away from the number, return "COLD!"
3. On all subsequent turns, if a guess is
closer to the number than the previous guess return "WARMER!"
farther from the number than the previous guess, return "COLDER!"
4. When the player's guess equals the number, tell them they've guessed correctly and how
many guesses it took!

You can try this from scratch, or follow the steps outlined below. A separate Solution notebook has
been provided. Good luck!

First, pick a random integer from 1 to 100 using the random module and assign it to a
variable

Note: random.randint(a,b) returns a random integer in range [a, b] , including both end
points.

In [ ]:

Next, print an introduction to the game and explain the rules

In [ ]:

Create a list to store guesses

Hint: zero is a good placeholder value. It's useful because it evaluates to "False"

In [ ]:

Write a while loop that asks for a valid guess. Test it a few times to make sure it
works.

localhost:8888/notebooks/Desktop/Python/Complete-Python-3-Bootcamp-master/02-Python Statements/09-Guessing Game Challenge.ipynb 1/2


6/15/2019 09-Guessing Game Challenge

In [ ]: while True:

pass

Write a while loop that compares the player's guess to our number. If the player guesses
correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and
continue asking for guesses.

Some hints:

it may help to sketch out all possible combinations on paper first!


you can use the abs() function to find the positive difference between two numbers
if you append all new guesses to the list, then the previous guess is given as guesses[-2]

In [ ]: while True:

# we can copy the code from above to take an input

pass

That's it! You've just programmed your first game!

In the next section we'll learn how to turn some of these repetitive actions into functions that can
be called whenever we need them.

Good Job!

localhost:8888/notebooks/Desktop/Python/Complete-Python-3-Bootcamp-master/02-Python Statements/09-Guessing Game Challenge.ipynb 2/2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy