How to use raw_input with argv?
Asked Answered
C

9

5

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

Curie answered 4/9, 2014 at 21:25 Comment(4)
You can't pass raw_input()! The following works fine for me: python test.py 1 2 3Dieppe
What operating system are you using?Canova
When you pass raw_input() it's not actually going to allow raw input. From the command line it's going to pass arg1, arg2 and arg3 from argv the string raw_input, arg0 will still remain the script name. However that should not throw an error since argv will produce valuesVarro
To fix the "too many values" exception, you really need to print argv and see what's there. That still won't solve the problem @ZWiki raised (command-line arguments are just strings, not code to be executed, so you're just going to get the string 'raw_input()' as the value of first, etc.—assuming your shell doesn't try to interpret that as a function call—bash will, but cmd won't, for example), but it will let you get past the exception so you can get to that problem, which is progress.Tugboat
C
7

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.

Canova answered 4/9, 2014 at 21:36 Comment(0)
F
11

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)
Freddafreddi answered 23/11, 2014 at 18:44 Comment(0)
C
7

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.

Canova answered 4/9, 2014 at 21:36 Comment(0)
C
2

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?

Conchaconchie answered 8/2, 2016 at 2:39 Comment(0)
C
0

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 + "? ")
Clippard answered 3/6, 2015 at 18:11 Comment(0)
E
0

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.

Ecumenicity answered 29/1, 2016 at 8:36 Comment(0)
R
0

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]
Reduction answered 2/2, 2016 at 7:32 Comment(0)
T
0

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."
Tuner answered 22/3, 2016 at 14:21 Comment(0)
C
0

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)
Countertenor answered 16/9, 2022 at 19:49 Comment(0)
R
-1

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
Riegel answered 7/10, 2015 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.