4 Replies - 2197 Views - Last Post: 29 August 2012 - 11:02 AM Rate Topic: -----

#1 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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 = num1 * num2



'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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • 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)
input_var == answer



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.
Was This Post Helpful? 2
  • +
  • -

#5 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1