Whenever I give a number through the command line, let’s say 92, it only takes the 9, completely ignoring the 2. Yet, if I use arg[1] it will use the 2 instead of the 9.
import sys
for arg in sys.argv:
print arg
print ""
print "-----"
print ""
try:
argNumber = int(arg[0])
except ValueError:
argNumber = 0
print "This is wrong"
for i in range(argNumber, 0, -1):
print i
Also, for some reason I can’t add a print “This is wrong” line to the except ValueError. It gives me an indention error?
The issue is that you are calling int on
arg[0]. Now,argis what’s left over from the initial loop, when you went through all the arguments insys.argv– it’s the last element from that loop. So, when you slice it with[0], you are in fact getting the first character from the last argument, rather than – as you intended – the first argument.Your fix is to simply use
sys.argv[1]there instead.