命令行-int()参数必须是字符串,类似字节的对象或数字,而不是“列表” [英] command line - int() argument must be a string, a bytes-like object or a number, not 'list'

查看:127
本文介绍了命令行-int()参数必须是字符串,类似字节的对象或数字,而不是“列表”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的代码:

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),而不是列表的一部分 sys.argv 从索引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])

这篇关于命令行-int()参数必须是字符串,类似字节的对象或数字,而不是“列表”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆