I'd like the program to exit if the input number is less than 0, but sys.exit() isn't doing the trick. This is what I have now:
if len( sys.argv ) > 1:
number = sys.argv[1]
if number <= 0:
print "Invalid 开发者_开发问答number! Must be greater than 0"
sys.exit()
Your test is failing because number is a string.
>>> '-1' <= 0
False
You need to convert number
to an integer:
number = int(sys.argv[1])
Note that in Python 3.0 your code would have given an error, allowing you to find your mistake more easily:
>>> '-1' <= 0
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
'-1' <= 0
TypeError: unorderable types: str() <= int()
精彩评论