0

So I'm reading a book and I saw that he did this:

from sys import argv

script, first, second, third = argv

print ("The script is called: ", script)
print ("Your first variable is: ",first )
print ("Your second variable is: ", second)
print ("Your third variable is: ", third)

and it give this error but in the book it works:

Traceback (most recent call last):
  File "c:\Users\FORCE 5 CORE\Desktop\visual studio code file\1\ex1.py", line 123, in <module>
    script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 1)
  • 1
    Does this answer your question? [sys.argv\[1\] meaning in script](https://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script) – CoderCharmander Jan 19 '21 at 14:40

2 Answers2

1

The script is trying to get 4 arguments passed to it, but you didn't pass any, so, argv only has the script name, because it is provided by your OS (hence, "expected 4, got 1")

You need to run the script with 3 more arguments, for example:

python ex1.py val1 val2 val3
PauloRSF
  • 26
  • 2
0

It is the Command Line Argument for Python. You pass these values with while calling the program. It is basically an Array.

script, first, second, third = argv

When you call your function from terminal with these arguments script, first, second, third = argv like:

>>python program_name.py 1 2 3

You are basically assigning script as program_name.py , first as 1, second as 2 , and third as 3.

To avoid getting the error, you have to provide all arguments while running the program from terminal.

itsDV7
  • 854
  • 5
  • 12