Reputation: 437
I'm new to python and i'm just reading practicing different things and i'm trying to figure out why argv isn't working for me
from sys import argv
script, bike, car, bus = argv
print ("The script is called:"), script
print ("The first variable is:"), bike
print ("The second variable is "), car
print ("Your third variable is : "),bus
I'm getting an error of need more than 1 value to unpack
Traceback (most recent call last):
File "ex13.py", line 6, in <module>
script, bike, car, bus = argv
ValueError: need more than 1 value to unpack
I am running my example program from the command-line by calling:
python ex13.py
Upvotes: 0
Views: 1625
Reputation: 19030
Your example is better written as (to cope with arbitrary usages):
from sys import argv
script, args = argv[0], argv[1:]
print("The script is called: ", script)
for i, arg in enumerate(args):
print("Arg {0:d}: {1:s}".format(i, arg))
The reason you'd be getting an error (place show Traceback) is because you're calling your script with fewer arguments than you are trying to "unpack".
See: Python Packing and Unpacking and Tuples and Sequences where it says:
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
To demonstrate what's going on with your example adn eht error you get back:
>>> argv = ["./foo.py", "1"]
>>> script, a = argv
>>> script
'./foo.py'
>>> a
'1'
>>> script, a, b = argv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
The error should be obvious here; You are trying to unpack more values than you have!
To fix your example I'd do this:
from sys import argv
if len(argv) < 4:
print ("usage: {0:s} <bike> <car> <bus>".format(script))
raise SystemExit(-1)
script, bike, car, bus = argv
print ("The script is called:", script)
print ("The first variable is:", bike)
print ("The second variable is ", car)
print ("Your third variable is : ", bus)
Update: I just noticed this; but all your print()
(s) are wrong. You need to either use str.format()
or put the argument inside the print()
function.
Upvotes: 7
Reputation: 365747
Pycharm is an ide and i just click run and it runs the program, but in powershell i type in python ex13.py and that runs the program
OK, then you aren't passing any arguments. So, what were you expecting to find as as the first, second, and third arguments? PowerShell isn't going to guess what bike, car, and bus you wanted to pass the program, any more than it's going to go out and buy you a bike, car, and bus. So, if you want to run this program with arguments representing your bike, car, and bus, you have to actually do that:
python ex13.py CR325 Elise VW
Then your script will output those arguments.
Well, actually, it may not, because your print
calls are wrong. If this is Python 2.7, those parentheses don't do anything, so you'll see:
The script is called: ex13.py
The first variable is: CR325
The second variable is Elise
The third variable is : VW
If it's Python 3.x, the parentheses wrap the arguments to the print
, just like any other function, so the , script
and so forth aren't part of the print
, so you'll just see:
The script is called:
The first variable is:
The second variable is
The third variable is :
Upvotes: 3