1

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!

Landon G
  • 819
  • 2
  • 12
  • 31
  • 1
    Doing `sys.argv[1:]` vs `sys.argv[1]` yields two different results. One will return the sliced list as a list with the remainder given to you. The other one will give you the object you specifically asked for. [here's more details on slicing and lists](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Torxed Feb 04 '19 at 19:42

1 Answers1

3

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])
blhsing
  • 91,368
  • 6
  • 71
  • 106