A program that generates a random number from 0 to 9 and asks the user to guess it.
Three chances are given to the user.
The program asks the user’s first guess.
If the first guess is equal to the random number then the user wins and the program ends.
If the first guess is less than the random number, the program tells the user that the number is greater than his/her guess.
If the first guess is greater than the random number, the program tells the user that the number is less than his/her guess.
Then the program asks the user for a second guess.
If the 2nd guess is equal to the random number then the user wins and the program ends.
If the 2nd guess is less than the random number, the program tells the user that the number is greater than his/her guess.
If the 2nd guess is greater than the random number, the program tells the user that the number is less than his/her guess.
Then the program asks the user for the third and last guess.
If the 3rd guess is equal to the random number then the user wins and the program ends.
If not the user loses, the random number is printed and the program ends.
It starts like this
from random import *
num = randint(0, 9)
print ("I chose a number from 0 to 9 and I want you to guess it.")
print ("You have three tries to guess the number.")
guess1 = input("1st guess: ")
from random import *
num=randint(0,9)
print (“I chose a number from 0 to 9 and I want you to guess it.”)
I chose a number from 0 to 9 and I want you to guess it.
print (“You have three tries to guess the number.”)
You have three tries to guess the number.
random_number = randrange(0,9)
count = 0 #Let the games begin!
while count < 3: #ask user to take a guess
guess = int(raw_input("Enter a guess: ")) #print the anwer to test the winning condition
if guess ==random_number:
print ‘Congratulations! You win!’ #end the game
break
else: #print this message only on the first 2 attempts
if count < 2:
print ‘Try again. You have ‘+str(2-count)+’ guesses left’ #proceed to the next guess attempt
count +=
SyntaxError: invalid syntax
while count < 3: #ask user to take a guess
guess = int(raw_input("Enter a guess: ")) #print the anwer to test the winning condition
if guess ==random_number:
print ‘Congratulations! You win!’ #end the game
break
else: #print this message only on the first 2 attempts
if count < 2:
print ‘Try again. You have ‘+str(2-count)+’ guesses left’ #proceed to the next guess attempt
count += 1
else:
SyntaxError: invalid syntax
>>> while count < 3:
#ask user to take a guess
guess = int(raw_input("Enter a guess: "))
#print the anwer to test the winning condition
if guess ==random_number:
print 'Congratulations! You win!'
#end the game
break
else:
#print this message only on the first 2 attempts
if count < 2:
print 'Try again. You have '+str(2-count)+' guesses left'
#proceed to the next guess attempt
count += 1
else:
print 'Sorry, you lose. The number was ' + str(random_number)
Enter a guess: 2
Try again. You have 2 guesses left
Enter a guess: 3
Congratulations! You win!
random_number = randrange(0,9)
count = 0 #Let the games begin!
while count < 3: #ask user to take a guess
guess = int(raw_input("Enter a guess: ")) #print the anwer to test the winning condition
if guess ==random_number:
print ‘Congratulations! You win!’ #end the game
break
else:
count += 1
print “Try again. You have {:d} guesses left”.format(count)
else:
print 'Sorry, you lose. The number was ’ + str(random_number)
Traceback (most recent call last):
File “<pyshell#19>”, line 1, in
while count < 3:
NameError: name ‘count’ is not defined
nter a guess: 5
Try again. You have 1 guesses left
Enter a guess: 6
Try again. You have 2 guesses left
Enter a guess: 8
Try again. You have 3 guesses left
Sorry, you ran out of chances. The number is 7
I can also make mistakes, i am just a human being.
Do you understand why starting count (which you could rename to guesses_left) is a slightly more logic approach? This way, we don’t have to do math when we want to print the number of remaining guesses
what if I put something before that like:
I chose a number for 0 to 9, I want you to guess it.
You have three tries to guess the number
1st guess: 5
Sorry your guess is wrong but I’ll give you a hint
My number is greater than your guess
Something like that uh hehe i’ll try to google something like that
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.
from random import randrange
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess ==random_number:
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print 'Congratulations! You win!'
break
if guess < random_number:
count -= 1
print 'Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
if guess > random_number:
count -= 1
print 'Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print 'Congratulations! You win!'
break
if guess < random_number:
count -= 1
print 'Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print 'Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)
'Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.
from random import randrange
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess ==random_number:
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print 'Congratulations! You win!'
break
if guess < random_number:
count -= 1
print 'Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
if guess > random_number:
count -= 1
print 'Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print 'Congratulations! You win!'
break
if guess < random_number:
count -= 1
print 'Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print 'Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print 'Congratulations! You win!'
break
if guess < random_number:
count -= 1
print 'Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
print 'Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)
SyntaxError: invalid syntax
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print 'Congratulations! You win!'
break
elif guess < random_number:
count -= 1
print 'Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print 'Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)