tags:

views:

336

answers:

6

Hello

I'm learning Python, and I'm having trouble with this simple piece of code:

a = raw_input('Enter a number: ')

if a > 0:
    print 'Positive'
elif a == 0:
    print 'Null'
elif a < 0:
    print 'Negative'

It works great, apart from the fact that it always prints 'Positive', no matter if i enter a positive or negative number or zero. I'm guessing there's a simple solution, but i can't find it ;-)

Thanks in advance

+2  A: 

raw_input is stored as a string, not an integer.

Try using a = int(a) before performing comparisons.

Jweede
+5  A: 
raw_input

returns a string so you need to convert a which is a string to an integer first: a = int(a)

DoR
+7  A: 

That's because a is a string as inputted. Use int() to convert it to an integer before doing numeric comparisons.

a = int(raw_input('Enter a number: '))
if a > 0:
    print 'Positive'
elif a == 0:
    print 'Null'
elif a < 0:
    print 'Negative'

Alternatively, input() will do type conversion for you.

a = input('Enter a number: ')
cpharmston
+1  A: 

raw input will return a string, not an integer. To convert it, try adding this line immediately after your raw_input statement:

a = int(a)

This will convert the string to an integer. You can crash it by giving it non-numeric data, though, so be careful.

Also works fine.
trolle3000
+6  A: 

Because you are using raw_input you are getting the value as a String, which is always considered greater than 0 (even if the String is '-10')

Instead, try using input('Enter a number: ') and python will do the type conversion for you.

The final code would look like this:

a = input('Enter a number: ')
if a > 0:
    print 'Positive'
elif a == 0:
    print 'Null'
elif a < 0:
    print 'Negative'

However, as a number of folks have pointed out, using input() may lead to an error because it actually interprets the python objects passed in.

A safer way to handle this can be to cast raw_input with the desired type, as in:

a = int( raw_input('Enter a number: '))

But beware, you will still need to do some error handling here to avoid trouble!

Justin Standard
Thanks! Did the job ;-)
trolle3000
The reason input() works this way is because whatever is typed by the user is evaluated by the interpreter... which is very unsafe. A search of the internet will provide numerous reasons why, but suffice it to say that doing int(raw_input()) within a try-catch should be considered better practice.
nilamo
True, you can find discussion on this here: http://mail.python.org/pipermail/tutor/2003-January/019634.htmlThe initial example is useful for getting a person who is new to python going. Once you start to consider error handling, you'll want to validate any and all user input, and at that point raw_input(...) will prove more useful.
Justin Standard
Agreed. The whole try-catch thing could be confusing to newbies.
nilamo
using `input` is not safe. Try using `a = int(raw_input('Enter:'))` instead
fengshaun
+1 for having both approaches.
nilamo
+7  A: 

Expanding on my comment on the accepted answer, here's how I would do it.

value = None
getting_input = True

while getting_input:
    try:
        value = int(raw_input('Gimme a number: '))
        getting_input = False
    except ValueError:
        print "That's not a number... try again."

if value > 0:
    print 'Positive'
elif value < 0:
    print 'Negative'
else:
    print 'Null'
nilamo
you dont' need getting_input here, you can just use 'while not value'. :P
monkut
While true, I prefer being fairly explicit in what goes on, so the intent of the code is obvious at a glance, without any additional thought.
nilamo