Argument Variables and ValueErrors in Python

Tiffany White,

Going through more Zed Shaw tutorials with Learn Python the Hard Way and finding it excellent as it gets more challenging. We are working with argument variables, which hold the arguments you pass through your python script until called later.

He let us know that part of the command:

was an argument, that part being:

We then wrote a script that accepted arguments. First, the first line of the script was:

Importing lets you add modules to your python script. Instead of piling in modules, python let’s you choose which ones you want. In this case we have the sys or system module. I am not sure what a system module is or what modules do yet, as I am at exercise 14 and still working through it.

Next we have the argument variables:

We call these variables, I am not sure if the correct word is call, I know it is for object-oriented programming, and I am not sure if python relies on objects like Ruby. But anyway…

This was the part of the script that would run when called in terminal:

When first running this in terminal I got a ValueError returned to me:

It seemed that I didn’t reference enough arguments. I then reread the tutorial and saw that I needed to specify the arguments by typing in a random name:

I replicated this according to the tutorial, trying to understand what the error was. I was a bit confused. I thought that I had four arguments, not five when I typed the following in:

I was literally stuck on this. Went into the Code Newbie Slack team looking for help and voila! I got some from the truly smart people there. I forgot that ex13.py was an argument as well. So I did only have four arguments. In the last example, my three unpacked arguments were, ex13.py, first, and 2nd. I needed one more, not 3. It may sound weird but my mind was lost.

So now that I know that I moved onto exercise 14, mixing in prompts with argument variables, format variables, and the like.

Here is the script:

Took me a bit to get that I was assigning a prompt to variables. I didn’t get that until I got to the last code block.

And here is another gif of the result:

© tiff.RSS