0

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.

1
  • 1
    argv is a list, you are currently assigning the whole list to all of the parameters. You need to use indexing to pass specific items in the list to the variables (e.g. argv[1]). Try read through this effbot.org/zone/python-list.htm Commented Mar 30, 2015 at 10:09

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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]

Comments

1

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].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.