= argv no enough values to unpack

#1

Hi guys,

I keep getting this recurring error that has prevented me from running ex 17, 20 and now 23.

script, input_encoding, error = sys.argv
ValueError: not enough values to unpack (expected 3, got 1)

I have looked online, copied Zed’s text but still no luck.
Any help would be much appreciated.
Thanks in advance.

Redtreble

#2

Hello and welcome to the forum@Redtreble

To me it looks like you did not provide some arguments when calling this file.
The variables ”input_encoding” and ”error” need something to process.
This you write in after the name of the file.

ex20.py first_argument second_argument

In this exercise there are text files to provide if I remember this correctly?

#3

Hi ulfen69,

Thanks for your response. I have just typed ‘python ex20.py first_argument second_argument’ into powershell still get this error:

File “ex20.py”, line 3, in
script, input_file = argv
ValueError: too many values to unpack (expected 2)

Any suggestions?

Redtreble

#4

Hi @Redtreble

I see now I forgot to write in Python in the example. Sorry for this.
The ”first_argument” is supposed to be a text file.
I think the other argument too should be a text file.

So it should look like:

python ex20.py inp.txt error.txt

#5

Hi @ulfen69,

Ok, I just typed “python ex20.py inp.txt error.txt.” into PowerShell, see screenshots attached, but still no joy.

#6

The text file you put in after Python must exist in your PC.
In the same directory as ex20.py

”First_argument” and ”inp.txt” was just examples.

#7

Hi @Redtreble, you need to take some more time to figure things out before coming to ask for help. In this example you seem to get an error, immediately stop, ask for help, then blindly type the help and repeat the process. To get better at programming you must spend time to figure out why something is broken and try to do things to fix it on your own before you ask for help. Otherwise you won’t learn anything or be able to work on your own or make anything on your own.

Let’s take this for example. You got the error “too many values to unpack”. When you get an error you google for it, so let’s see what you get when you google for “python too many values to unpack”:

https://www.google.com/search?client=firefox-b-1-d&q=python+too+many+values+to+unpack

I know people who code all day by just doing that. That should be your first step. Second step is to go back to the exercise and re-read it, carefully. In this example, I tell you how to run it, you’re just not seeing it. Go look. I run it differently from you and explain what’s going on.

The next thing to do is to try and figure out what the message means. “Too many values…” that means you are making too many variables. “To unpack…” if you read some of the things above in the google search you start to find out that what’s happening is called “unpacking”. Bascially, it’s this:

  1. The equal sign is assignment here, and it rules everything.
  2. The left of the equals sign you have a list: script, input_file
  3. The right of the equals sign you have a variable.
  4. There are two items in the list on the left.
  5. That means there is probably MORE THAN TWO things inside argv on the right.
  6. The “unpacking” means python is taking whatever is in argv, and unpacking it into each variable on the left, and your argv is too large.

Alright, so what’s going into argv?

python ex20.py first second

Try this:

print(argv)

Put that right above the line that’s causing the error. Any time you get an error with some variables, print them out and see what’s inside them. You’ll find out that argv has THREE contents in a list, but you only have TWO variables on the left. Thus, too many variables are in argv to unpack into your two on the left.

Now, read through all of that and spend some time to figure out what’s going on before you come back. This isn’t to rush you off and make you feel dumb, but more to help you get better at programming since programming is ALLLLLLLLLL about solving complex problems on your own.

Let us know when you get it.

#8

Hi @zedshaw @ulfen69
I’ve been working on this today and actually went back to ex13 where argv is first discussed. I understand that it is a balancing act with the ’ = ’ as the fulcrum. I learned a few things today by reviewing ex13 - 16 for the 3rd time but coding still hasn’t clicked for me. I only got an error when I typed print(argv) in line 2 of ex20. I have since been able to run ex20 on powershell by adding ‘test.txt’ on the command line but it doesn’t read as it should. I’ve even stooped to copying and pasting your code into atom and running it but got the same results. See attached.
image

#9

Awesome, you’re getting it now. You won’t understand programming at ex13 at all. Probably won’t really understand it for a while, so just keep going and get over the middle hump. Everything feels like garbage in the middle up but if you keep pushing it eventually clicks.

But, I recommend you re-read this sentence you wrote:

“I only got an error when I typed print(argv) in line 2 of ex20.”

Alright, but I was recommending to you that you take more time to figure out why and don’t just blindly type code in and expect it to work. When you type print(argv), you’re telling python to print out the argv variable. HOWEVER, if you haven’t created the argv variable yet then you’ll get an error. That line has to come after you’ve imported argv, then it’ll work. So, in this case, you should have tried to put it on a different line and see if that works.

But, you’re doing fine for a beginner. I’m just pushing you on doing more of your own work so that you get into the habit of trying to solve the problems first, since that’s the only way you learn to code well enough to do it.

Also, when you got the error for print(argv) did you google it? Again, google the error, look at what you wrote, confirm 3 times it’s exactly the same, and try doing it differently. Until you do those things it’s no use asking for help.

A free service run by Zed A. Shaw for learncodethehardway.org.