## guessing game doesnt compare my input with a random number

This is the place for queries that don't fit in any of the other categories.

### guessing game doesnt compare my input with a random number

for some reason my game won't tell me when i guess the number correctly, someone help please.
Here is the code:
Code: Select all
import random
import sys
import time

enter = "Please press enter to continue..."

print("Hello! Welcome to a guessing game!")
print("Please guess a number between 1 - 100!")
computernum = random.randint(1, 100)
guess1 = input("My first guess is: ")
if guess1 == computernum:
time.sleep(1)
print("you win!")
input(enter)
sys.exit(0)
else:
time.sleep(1)
print("sorry, try again!")
print("You have two guesses remaining!")
guess2 = input("my second guess is: ")
if guess2 == computernum:
time.sleep(1)
print("Wow!, congratulations! you win!")
input(enter)
sys.exit(0)
else:
time.sleep(1)
print("Sorry, one more try!")
print("YOU HAVE ONE MORE TRY!")
guess3 = input("My final guess is: ")
if guess3 == computernum:
time.sleep(1)
print("congratulations! You Win!")
input(enter)
sys.exit(0)
else:
time.sleep(1)
print("Sorry, you lose the game!")
print ("the computer's number was " , str(computernum))
input(enter)
sys.exit(0)
Last edited by Yoriz on Thu Jun 13, 2013 11:06 pm, edited 1 time in total.
Reason: edited the title
jack

Posts: 5
Joined: Thu Jun 13, 2013 10:29 pm

### Re: guessing game

Code: Select all
computernum = random.randint(1, 100)
print(type(computernum))
<class 'int'>
Returns a int

Code: Select all
guess1 = input("My first guess is: ")
print(type(guess1))
<class 'str'>
Returns a str

Code: Select all
if guess1 == computernum:
is comparing a int to a str which will never be the same
you need to compare them as the same type
by either

Code: Select all
guess1 = int(guess1)
turning the str into a int

Code: Select all
computernum = str(computernum)
or the int into a str

before comparing them
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1459
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: guessing game doesnt compare my input with a random numb

thanks that works perfectly!
jack

Posts: 5
Joined: Thu Jun 13, 2013 10:29 pm