0% found this document useful (0 votes)
10 views1 page

Game Guess

Uploaded by

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

Game Guess

Uploaded by

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

Your own while / else

Now you should be able to make a game similar to the one in the last exercise. The
code from the last exercise is below:

count = 0
while count < 3:
num = random.randint(1, 6)
print num
if num == 5:
print "Sorry, you lose!"
break
count += 1
else:
print "You win!"

In this exercise, allow the user to guess what the number is 3 times.

guess = int(raw_input("Your guess: "))

Remember, raw_input turns user input into a string, so we use int() to make it a
number again.
Instructions
Checkpoint 1 Passed

1.

Use a while loop to let the user keep guessing so long as guesses_left is greater
than zero.

Ask the user for their guess, just like the second example above.

If they guess correctly, print "You win!" and break.

Decrement guesses_left by one.

Use an else: case after your while loop to print "You lose.".

from random import randint

# Generates a number from 1 through 10 inclusive


random_number = randint(1, 10)

guesses_left = 3
# Start your game!
while guesses_left > 0:
guess = int(raw_input("Your guess: "))
if guess == random_number:
print "You win!"
break
guesses_left -= 1
else:
print "You lose."

print random_number

You might also like