# Checking integer equality problem

Page 1 of 1

## 4 Replies - 3300 Views - Last Post: 29 August 2012 - 11:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=290341&amp;s=0b8f32d2d4473a4ea93c34b1ca82f74f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cmwise

Reputation: 5
• Posts: 169
• Joined: 14-February 09

# Checking integer equality problem

Posted 29 August 2012 - 08:00 AM

Hey guys, I'm just beginning to learn Python and all of its syntactical subtleties, but this one I'm stumped on (even after googling the problem extensively).

Basically, I'm reading input from the user, and checking if it's equivalent to a value I've stored.
```num1 = random.randrange(0, 10, 1);
num2 = random.randrange(0, 10, 1);

```

'answer' is the value I'm checking user input against.

```input_var = input("? ")

```

I get input using the... well, input function? (I'm assuming this is the correct way to retrieve user input interactively, however if not, please enlighten me!!)

```if input_var == answer:

```

When checking for equality this way, even if the number the user input is the same number as what was stored in 'answer', the program always enters the 'else:' clause instead.

I'm not sure what's going on, I've printed out both 'input_var' and 'answer' and they display the same number (assuming the user inputs the same number).

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: Checking integer equality problem

### #2 sepp2k

• D.I.C Lover

Reputation: 2271
• Posts: 3,486
• Joined: 21-June 11

## Re: Checking integer equality problem

Posted 29 August 2012 - 08:05 AM

If you're using Python 3, the input function will always return a string. Strings are never equal to numbers (or any other type) in Python (so 42 is not equal to "42" for example).

So if you want to compare a string to an integer, you either need to convert the string to an integer (using int) or the integer to a string (using str) before you compare them.

### #3 cmwise

Reputation: 5
• Posts: 169
• Joined: 14-February 09

## Re: Checking integer equality problem

Posted 29 August 2012 - 08:13 AM

Thanks for the response, sepp2k

Unfortunately, trying to call
``` int(input_var)
```
didn't fix the issue. I also tried to use the same method to convert 'answer' to an int, in case it wasn't for some reason.

Am I not using the int conversion correctly? This is fairly bizarre.

### #4 sepp2k

• D.I.C Lover

Reputation: 2271
• Posts: 3,486
• Joined: 21-June 11

## Re: Checking integer equality problem

Posted 29 August 2012 - 08:38 AM

Did you try it like this:

```int(input_var) == answer
```

Or like this:

```int(input_var)

```

If the latter: int (like all conversion functions) returns the converted value - it does not change its argument directly. So calling int without doing anything with its result won't have any effect.

### #5 cmwise

Reputation: 5
• Posts: 169
• Joined: 14-February 09

## Re: Checking integer equality problem

Posted 29 August 2012 - 11:02 AM

I actually tried that right after posting :X

Thanks for your help, everything's working now!