4

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

6 Answers 6

7

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!

Sign up to request clarification or add additional context in comments.

4 Comments

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.
True, you can find discussion on this here: mail.python.org/pipermail/tutor/2003-January/019634.html The 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.
Agreed. The whole try-catch thing could be confusing to newbies.
using input is not safe. Try using a = int(raw_input('Enter:')) instead
7

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: ')

Comments

7

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'

2 Comments

you dont' need getting_input here, you can just use 'while not value'. :P
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.
5
raw_input 

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

Comments

2

raw_input is stored as a string, not an integer.

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.