Ex.13 of LPTHW ValueError occured

I wrote the program as given in the book in windows powershell

from sys import argv
script, first, second, third = argv

after writing this I got an error which states as follows:

ValueError: not enough values to unpack (expected 4, got 1)

what should I do now?

Hi @Dash_Ashutosh9876543

In this lesson you give python some arguments when starting the code.
It is called command line arguments.
In contrast to the input variables that got its arguments while the code is running.

So you have to give it something for the
first,
second,
third variables.
example:

python3 ex13.py uno dos tres

As you can see it already got one value.

That is the first variable (script) wich is assigned to the name of the file.

I can only guess it pick the first variable for the name of the file.
This is interesting. So if anyone can give an explanation I am all ears.

Did you give command line arguments? It’s sound like you didn’t with the Value Error.

Try running the following from command line:

Python3 ex13.py first_arg second_arg third_arg

@gpkesley can u explain briefly, I couldn’t understand. I pasted the code in powershell and I gives SyntaxError. why is this python 3.x ex.xx everywhere?

The code you written in the file (ex13.py) says to import a python module called argv.

This module manages values that you add to the command line when you execute the file. There’s different types of arguments but you’ll get to that later.

When you add these arguments to the command line as in the examples above, at runtime they need somewhere to go. The line written into the file that equals argv, is where the arguments get unpacked.

As mentioned above, the first argument relates to the ex13.py script, (hence the name but actually you can call it anything). The first, second, third variable labels in the book (or uno, dos, tres in @ulfen69 example or first_arg, second_arg, third_arg in my example) and where the command line arguments get unpacked to. This process is managed by argv, which is why you need to import it.

I expect the syntax error you got was my typo but capitalising ‘python3’ as ‘Python3’. Sorry about that!

1 Like

Also @Dash_Ashutosh9876543 the need to keep writing python ex.xxx everywhere is you instructing your computer to run a python file.

You write the file and save it. But your computer needs to know what to do with it. You could have lots of different languages installed on your computer, like Ruby, JavaScript, etc so you need to be specific about what to run that file with. Remember the computer is dumb so you need to be explicit and tell it:

‘Using Python framework version 3, run this ex13.py file’.

Imagine you wanted to open that file in your text editor, you could do that from the command line too. Just replace ‘python’ with the editors name and the file (depends on which editor you use but ‘notepad ex13.py’ should work I think).

Does that help?

I used to do that by only typing python in cmd and that’s it… Moreover cmd doesn’t recognizes file name…
What about my previous problem?what should i do?

Hello @Dash_Ashutosh9876543

To me it looks like you are starting the python shell.

Does look like this:

Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux (not Linux on your pc)
Type "help", "copyright", "credits" or "license" for more information.
>>> 

?

@Dash_Ashutosh9876543 when you only use python on CMD you are opening the python interpreter which lets you interact with the python framework interactively. You can write small programmes here or hack about but it’s not a good idea to write an entire application like this (mostly because when you quit the session, everything in memory is disregarded).

It’s a common paradigm to call a piece of software and provide some parameters for it as instructions, from the command line, as you probably know.

$ git add “some_file.txt”
$ curl “http://www.some-website.com”

Hey @Dash_Ashutosh9876543, I’m guessing you didn’t watch the video for this exercise? Actually, you should probably go back and watch all the videos since they’re required for reading this book and understanding it.

But, you just need to run your script correctly. You are starting cmd. You should instead start PowerShell (BUT NOT POWERSHELL ISE).

Then, when you start powershell you should see this:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\Zed>

I have “Zed>” here but you might have “Dash>” instead on that last line. Now stop, and very carefully read that last line.

Did you stop and read it? Good, at the end there is > then a space then a blinking cursor. That’s where you type your commands. The command that @gpkesley and @ulfen69 are telling you to run are NOT python code. They are PowerShell commands. You then type this after the > and hit ENTER:

python ex13.py  apple orange grape

What this does is commands PowerShell to:

  1. Run the python.exe program, which is what runs your python code.
  2. Tell python.exe to load your ex13.py file.
  3. Tell your ex13.py file to process three arguments: apple orange grape
  4. ex13.py then puts those three words into your argv variable.

Now, this is mentioned in the exercise, so that means you’re also not reading the exercises carefully. You need to start reading them very carefully and when you have an error, go back and re-read them to see if you missed something. In fact, this exact problem should be called out in giant red warnings right before you run this command.

Lastly, you should go through the Appendix A in the book that teaches you the command line before you go any further. Not knowing the command line will hold you back in ALL programming so take the time to learn the basics.

Let us know if this helped you figure it out.

1 Like

The book is awesome . Sorry to ask , but can you tell me a way to run this on termux . Thank you

You can install Python with pkg install python. Then it should work like everywhere else, provided you have access to your Python files.

Screenshot_20200410-221357_Termux
:tipping_hand_man: thanks @florian

That’s a pretty cool setup. What font is that? What’s this app?

Don’t know about the font but It is termux app. I really was confused at first how to use it but when I came to know how to use it , it’s awesome . Im Learning python without a laptop once this quarantine ends going to buy once asap . Thanks @zedshaw ,you really got me into this … Python is fun.

Yes, I didn’t know about termux before, so thanks for posting your question!

It’s pretty cool, a working Linux on Android. I suppose you could easily install git and node and whatnot.

If only there was a decent mobile text editor…

You might be able to get one of these and be set for a while:

https://www.canakit.com/

Take a look and if you find a link to one you can afford that you think works then paste the link here and I’ll double check for you.