Reputation: 199
First of all, i'm very extremely noob, beginner etc. in python and programming in general, so sorry for the further questions but i have this sample code:
from sys import argv
p_script = argv
p_user_name = argv
p_id = argv
v_prompt = ": "
print "Hey %s, I'm the %s script with id: '%s'" % (p_user_name, p_script, p_id)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % p_user_name
v_likes = raw_input(v_prompt)
print "Where do you live %s?" % p_user_name
v_lives = raw_input(v_prompt)
print "What kind of computer do you have?"
v_computer = raw_input(v_prompt)
print """Alright, so you said '%s' about liking me.
You live in '%s'. Not sure where that is.
And you have a '%s' computer. Nice.""" % (v_likes, v_lives, v_computer)
and this:
from sys import argv
p_script, p_user_name, p_id = argv
v_prompt = ": "
print "Hey %s, I'm the %s script with id: '%s'" % (p_user_name, p_script, p_id)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % p_user_name
v_likes = raw_input(v_prompt)
print "Where do you live %s?" % p_user_name
v_lives = raw_input(v_prompt)
print "What kind of computer do you have?"
v_computer = raw_input(v_prompt)
print """Alright, so you said '%s' about liking me.
You live in '%s'. Not sure where that is.
And you have a '%s' computer. Nice.""" % (v_likes, v_lives, v_computer)
i really don't understand why the output is crazy, like after passing the arguments or parameters as strings into the code when running it in powershell (i.e: exec python "example 1", "Name", "101") it gives me after each stept on run-time all the arguments. So what i'm trying to ask here, is why it doesn't works as-well correctly on different style o parameter declaration. See pls the declaration field of the first code snippet and the second declaration field i.e the "classic" style. Many Thanks, cheers.
Upvotes: 0
Views: 59
Reputation: 31339
You are not using assignment correctly. You are assigning the list argv
over and over instead of unpacking it, or assigning a single item from it instead of the entire list.
This part:
p_script, p_user_name, p_id = argv
Is not equivalent to what you wrote, which assigns the list argv
over and over:
p_script = argv
p_user_name = argv
p_id = argv
It is, however, equivalent to this:
p_script = argv[0]
p_user_name = argv[1]
p_id = argv[2]
Upvotes: 2
Reputation: 14511
The problem is in the first lines:
p_script = argv
p_user_name = argv
p_id = argv
You probably meant to get elements from argv, like argv[0]
.
Upvotes: 1
Reputation: 42748
You found the python list unpacking. In your first script
p_script = argv
p_user_name = argv
p_id = argv
all three p_...
have the same value argv
. In the second script
p_script, p_user_name, p_id = argv
the list argv
is unpacked, so that the first element becomes p_script
, the second p_user_name
and the third p_id
.
Upvotes: 3