-3

I'm using Learning Python the Hard Way. In the process of my learning I came across this error. Though I have been trying to debug since yesterday, I couldn't. This is my code:

import sys
from sys import argv

script, first, second = argv
print('the script is called:', script)
print('the first variable is:', first)
print('the second vriable is:', second)
print('the third variable is:', third)
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • how did you try debugging this? what (and how many) arguments are you passing? – Nicolas Gervais Aug 03 '20 at 15:24
  • Does this answer your question? [Want to find contours -> ValueError: not enough values to unpack (expected 3, got 2), this appears](https://stackoverflow.com/questions/54164630/want-to-find-contours-valueerror-not-enough-values-to-unpack-expected-3-go) – Sowjanya R Bhat Aug 03 '20 at 15:25
  • Where is `third` supposed to have come from? – khelwood Aug 03 '20 at 15:25

2 Answers2

0

The error you're getting means you're trying to deconstruct an iterable (list, tuple) into more values than there are in the actual list. If you were to run

print(argv, len(argv))

You might see that you don't have three variables in argv.

argv shows the parameters you provided while running a script. So for example, if I ran:

python test_args.py

I would only receive: ['test_args.py'] as my argv variable. If I tried providing more parameters, like:

python test_args.py a b

Then my argv would look like: ['test_args.py', 'a', 'b']. Your code is completely dependent on how many parameters are passed when you are running it, so mess around with that as well to get a better sense of what is going on.

M Z
  • 4,571
  • 2
  • 13
  • 27
0

I suppose that you are executing the script in this format

python script.py 10 20

The problem is that you are using an extra variable third which is not initialized. If you really want 3 arguments, then you have to pass three values at execution time as follows.

python script.py 10 20 30

Then change the code as follows.

from sys import argv

script, first, second, third = argv
print('the script is called:', script)
print('the first variable is:', first)
print('the second vriable is:', second)
print('the third variable is:', third)

Thank you

Dinesh
  • 812
  • 4
  • 14