Reputation: 53
I'm doing ex13 from Learn Python The Hard Way
I'm trying to pass:
python ex13.py raw_input() raw_input() raw_input()
my code is below:
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
The error I keep getting is:
Traceback (most recent call last):
File "ex13.py", line 5, in <module>
script, first, second, third = argv
ValueError: too many values to unpack
I want to know why i'm getting this error and how to fix it
Upvotes: 5
Views: 13054
Reputation: 159
I am using Python 3.x version (So to take user input I simply use input() function)
Referring to LPTHW, I think the author wants us to use input() and argv separately.
Here is my example.
from sys import argv
script,first,second,third = argv
age = input("Enter your age")
print("The script is called:",script)
print("Your first variable is:",first)
print("Your second variable is:",second)
print("Your third variable is:",third)
print("Your age is",age)
Upvotes: 0
Reputation: 83
I'm learning this now, I think he just wants you to think and try more about it. Here is my code:
from sys import argv
script, slave = argv
print "Superme master, slave %s is waiting for your order." % slave
slave_says = "Humble %s wants know your supreme name, master:\n" % slave
name = raw_input(slave_says)
want = raw_input("What do you want, my superme master?\n")
if want == "Make you freedom.":
print "When you make me freedom. you are also make yourself freedom."
else:
print "Yes, my superme master."
Upvotes: 0
Reputation: 21
If you are trying to complete the Exercise 13 study drill from Learn Python the Hard Way, then you are trying to combine argv and raw_input(). The author suggests you use raw_input() to get more input from the user.
With help from this thread I came up with this:
from sys import argv
ScriptName, first, second, third = argv
print "What is your fourth variable?"
fourth = raw_input()
print "What is your fifth variable?"
fifth = raw_input()
print "What is your sixth variable?"
sixth = raw_input()
print "The script is called: ", ScriptName
print "Your first variable is: ", first
print "Your second variable is: ", second
print "Your third variable is: ", third
print "Your fourth variable is: ", fourth
print "Your fifth variable is: ", fifth
print "Your sixth variable is: ", sixth
print "For your script %r, these are the variables: %r, %r, %r, %r, %r,
and %r." % (ScriptName, first, second, third, fourth, fifth, sixth)
Does this seem to be what the author is suggesting?
Upvotes: 2
Reputation: 463
Yes you can do that, but it's a bit tricky and the author didn't mention it in previous exercises.
argv is actually a list[] (or data array), so you can address its individual elements within the script.
Here is my example:
from sys import argv
script, first, second, third = argv # one way to unpack the arguments
script_new = argv[0] # another way
first_new = argv[1]
second_new = argv[2]
third_new = argv[3]
print "original unpacking: ", script, first, second, third
print "argv[] unpacking: ",script_new, first_new, second_new, third_new
argv[0] = raw_input("argument 0? ")
argv[1] = raw_input("argument 1? ")
argv[2] = raw_input("argument 2? ")
argv[3] = raw_input("argument 3? ")
print argv[0], argv[1], argv[2], argv[3]
Upvotes: 0
Reputation: 1
Since the author is clear with his instruction in the study drill: "Combine raw_input
with argv
to make a script that gets more input from a user.", you can use add another line asking the fourth input.
fourth = raw_input("Enter the fourth value: ")
print "Your fourth variable is: ", fourth
Or looking at your code, i figure that you over-think it like "to make a script that gets input from a user", do like this
from sys import argv
script, first, second, third = argv, raw_input("Enter first value: "), raw_input("Enter second value: "), raw_input("Enter third value: ")
print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third
This way you don't need to supply python ex13.py
with data in order to run the script. Again, it is not what the study drill wants you to do, but I hope it might help.
Upvotes: 0
Reputation: 5585
the auther's idea should be like this if you are in ipython notebook
% run ex13.py 1st 2nd 3rd
if you are in command line
python ex13.py 1st 2nd 3rd
Upvotes: -1
Reputation: 1
I think this is what the author meant (taking an input using argv & using it int raw_input()) :-
from sys import argv
script, first, second, third, take_input = 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
raw_input(take_input + "? ")
Upvotes: 0
Reputation:
I'm currently going through LPTHW myself and just got to this exercise. I think what the author means is that he wants you to, just in the same script, use both argv
and raw_input()
. He doesn't mean for you to combine them, per se, in the same argument or line or whatever. In fact, one of the 'Common Student Questions' that he mentions deals with just this problem. He says
Don't overthink it. Just slap two lines at the end of this script that uses
raw_input()
to get something and then print it. From that start playing with more ways to use both in the same script.
Even though it's 2 months late, hope it helps.
This is how I've modified the script in order to complete the 'Study Drill' in question:
from sys import argv
script, first, second, third = argv
fourth = raw_input("What is your fourth variable? ")
print "All together, your script is called %r, your first variable is %r, your second is %r, your third is %r, and your fourth is %r" % (script, first, second, third, fourth)
Upvotes: 11
Reputation: 61597
You cannot "use raw_input()
with argv
". argv
is supplied with data that you specify before running the program. raw_input()
is a Python function, i.e. something that your program can do. The command line - where you type the python
command in order to run your program - is a completely separate thing from the program itself.
Upvotes: 7