Python 命令行-int()参数必须是字符串、类似字节的对象或数字,而不是';列表';
我得到了一个非常基本的代码:Python 命令行-int()参数必须是字符串、类似字节的对象或数字,而不是';列表';,python,command-line,sys,Python,Command Line,Sys,我得到了一个非常基本的代码: 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 total
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))
如果有人有任何建议,我们将不胜感激 方括号中的冒号表示前面列表对象的一部分。在本例中,您只需要第二个项目(索引为1),而不是列表的一部分
sys.argv
从索引1开始:
arg = int(sys.argv[1])
执行
sys.argv[1://code>vssys.argv[1]
会产生两种不同的结果。其中一个将以列表的形式返回切片列表,剩余部分将提供给您。另一个会给你你特别想要的东西。
>>> Python program.py 10
3628800
arg = int(sys.argv[1])