1
votes

Every time I run this program I get "ValueError: need more than 1 variable." But I'm doing what Zed says to do by running ex13.py first 2nd 3rd. I don't need to type python in the terminal before a filename because my computer recognizes python files. I'm on Windows 7 and using python 2.7. Any help would be appreciated. I've tried the most popular answer in this thread: ValueError: need more than 1 value to unpack , but I'm still getting the same error. Any help would be much appreciated

from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

Edit: Here's the error I'm getting: Traceback (most recent call last): File "C:\Users\Ian\lpthw\ex13.py", line 3, in script, first, second, third = argv ValueError: need more than 1 value to unpack

3
Can you copy/paste the entire output from python? - Winston Ewert
I added it to my question. - i32505
I'm going to guess here that whatever configured your computer to run .py files from the command line didn't configure it to pass along the command line arguments. Can you try explicitly calling python and passing it the script and arguments to see if I'm right? - Winston Ewert
It seems like your assumption is correct. I did what you said to do and it returned "'python' is not recognized as an internal or external command, operable program, or batch file." - i32505
@i32505 this thread talks about ways to add Python to your Path on Windows (apologies if you're using another OS): stackoverflow.com/a/4855685/3651800 - Matt Coubrough

3 Answers

3
votes

It's what Winston Ewert said in the comments. You have to tell your system where's python, otherwise it won't be able to run your script properly. For example, in Linux/Unix you achieve that using a shebang pointing to the python executable, like #!/usr/bin/python, or just running the script like python your_script. Try running it from the command like using python ex13.py first 2nd 3rd and you will see that it works.


As you are on Windows, here's how to properly configure Python on it:

3.3. Configuring Python

3.3.1. Excursus: Setting environment variables

3.3.2. Finding the Python executable

3.3.3. Finding modules

3.3.4. Executing scripts

~ Official Documentation

0
votes

you need to use terminal to do this excersise. lets say your 'ex13.py' is saved in a folder called 'coding' on 'desktop'.

in terminal, first you would need to write: cd desktop

then, you would need to write: cd coding

finally, you would write: python ex13.py first second third

if your file or folder or location is different, change this accordingly

-1
votes

This could be due to an error caused by incorrect copying from the Learn Python the Hard Way PDF. When you copy the code, make sure that you do not also copy the numbers on the left-hand side. If you copy the numbers too, that could cause your program to crash.