简体   繁体   English

麻烦简单的Python代码

[英]Trouble with simple Python Code

I'm learning Python, and I'm having trouble with this simple piece of code: 我正在学习Python,而我在使用这段简单的代码时遇到了麻烦:

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 提前致谢

That's because a is a string as inputted. 那是因为a是输入的字符串。 Use int() to convert it to an integer before doing numeric comparisons. 在进行数值比较之前,使用int()将其转换为整数。

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. 或者, input()将为您进行类型转换。

a = input('Enter a number: ')

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') 因为您正在使用raw_input您将获取值作为String,该值始终被视为大于0(即使String为'-10')

Instead, try using input('Enter a number: ') and python will do the type conversion for you. 相反,尝试使用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: '))

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:

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! 但要注意,你仍然需要在这里做一些错误处理以避免麻烦!

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'
raw_input 

返回一个字符串,所以你需要转换a是一个字符串为整数第一: a = int(a)

raw_input is stored as a string, not an integer. raw_input存储为字符串,而不是整数。

Try using a = int(a) before performing comparisons. 在执行比较之前尝试使用a = int(a)

raw input will return a string, not an integer. 原始输入将返回一个字符串,而不是整数。 To convert it, try adding this line immediately after your raw_input statement: 要转换它,请尝试在raw_input语句之后立即添加此行:

a = int(a) 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. 但是,你可以通过给它非数字数据来崩溃它,所以要小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM