command line - int() argument must be a string, a bytes-like object or a number, not 'list'

可紊 提交于 2019-12-24 15:53:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!