Page 1 of 1

# Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 01:22 PM

I'm making a number guessing game using the random module. Here's my code:

```import random

number = random.randint(0,10)
counter = 1

guess = input ("Guess a number between zero and ten. ")
if guess == 0:
print number
if guess == 11:
guess = number
if number == guess:
print "Good guess!"
quit()
else:
while counter <= 5:
counter = counter + 1
print "Guess no.", str(counter) + ":"
guess = input ("Try again! ")
else:
print "Game over."
```

As you see, I added two "hacks" that can be used when the program asks the first question. The problem is, even when I type in the known correct answer, the program tells me to try again. Answering "11" to the first question or "0" when it's the right answer have been the only ways to win when using random integers (so it seems that the program structure is correct).

The program also works when I assign a specific value to the variable "number" and then type it in.

So far I've tried declaring "guess" global when it's assigned inside the loop (and noticed it's not necessary) and changing line 11 to
```if int(number) == guess:
```

I think this is weird, as I have used random integers before without any problems.

This post has been edited by KorvusKoraks: 02 August 2011 - 01:23 PM

Is This A Good Question/Topic? 0

## Replies To: Number guessing with random module - integer comparison problem

### #2 Brewer Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 01:31 PM

One obvious problem is that when you get input it is always a string by default. So you need to cast it to an integer, like so:

```guess = int(input("Guess a number between 0 and 10: "))
```

This post has been edited by Brewer: 02 August 2011 - 01:31 PM

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 01:36 PM

Still not working... Thanks anyway.

### #4 Motoma Reputation: 452
• Posts: 798
• Joined: 08-June 10

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 01:40 PM

```>>> 5 == "5"
False

```

What you have here is a type difference. When you call input() you will get a string type back; when you call randint you will get an integer.

What you must do for your comparisons is cast one to the other; in your situation, I would cast the random number to a string in order to avoid user input type casting errors.

### #5 Motoma Reputation: 452
• Posts: 798
• Joined: 08-June 10

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 01:59 PM

Something I should have asked before I responded: What version of Python are you using? Python 3's version of input() behaves differently than Python 3's.

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 02:02 PM

I'm using Python 2.7.

So, I have tried three versions now.

First:
```import random

number = str(random.randint(0,10))
counter = 1

guess = input ("Guess a number between zero and ten. ")
if guess == "0":
print number
if guess == "11":
guess = number
if number == guess:
print "Good guess!"
quit()
else:
while counter <= 5:
counter = counter + 1
print "Guess no.", str(counter) + ":"
guess = input ("Try again! ")
else:
print "Game over."
```

Second:
```...
number = str(random.randint(0,10))
...
guess = str(input ("Guess a number between zero and ten. "))
...
guess = str(input ("Try again!"))
...
```

Third:
```...
number = str(random.randint(0,10))
...
guess = raw_input ("Guess a number between zero and ten. ")
...
guess = raw_input ("Try again!")
...
```

No success.

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 02:23 PM

Okay, the problem lies within the while loop of the counter - the program works perfectly when I modified the code like this:

```import random

number = random.randint(0,10)
counter = 1

guess = input ("Guess a number between zero and ten. ")
if guess == 0:
print number
if guess == 11:
guess = number
while number != guess:
# while counter <= 5:                     #    I
#   counter = counter + 1                 #    commented
#   print "Guess no.", str(counter) + ":" #    some lines
guess = input ("Try again! ")           # <- and decreased indent here
# else:
#   print "Game over."
else:
print "Good guess!"
```

Anyone smarter than me see the problem? This post has been edited by KorvusKoraks: 02 August 2011 - 02:23 PM

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 02:33 PM

Hey, I solved it! The while loops should have been nested the other way around.

```import random

number = random.randint(0,10)
counter = 1

guess = input ("Guess a number between zero and ten. ")
if guess == 0:
print number
if guess == 11:
guess = number
while counter <= 5:
while number != guess:
counter = counter + 1
print "Guess no.", str(counter) + ":"
guess = input ("Try again!")
else:
print "Good guess!"
break
else:
print "Game over."
```

Previously, the program asked me to try again regardless of the answer being equal to the random value.

This post has been edited by KorvusKoraks: 02 August 2011 - 02:35 PM

## Re: Number guessing with random module - integer comparison problem

Posted 02 August 2011 - 03:18 PM

A final correction - the second while loop should have been an if statement.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }