问题
I got a very basic code:
import sys
file = sys.argv[0]
arg = int(sys.argv[1:])
def multiplier(x):
if arg < 1:
print('Put an argument in')
else:
totals = 1
for i in range(1,x+1):
totals *= i
return totals
print(multiplier(arg))
And I'm trying to run this from the command line and I keep getting this error:
File "program.py", line 4, in <module>
arg = int(sys.argv[1:])
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'list'
I understand the error, but I'm new to the command line so I'm a bit confused in the command line context.
If all went well, I'd expect something like this (Input/output):
>>> Python program.py 10
3628800
If anyone has any suggestions, it would be much appreciated!
回答1:
A colon in square brackets denotes a slice of the preceding list object. In this case, you want just the second item (with index 1), rather than a slice of the list sys.argv
start from index 1:
arg = int(sys.argv[1])
来源:https://stackoverflow.com/questions/54523228/command-line-int-argument-must-be-a-string-a-bytes-like-object-or-a-number