Problem Explaining 'argv'

Posted 25 July 2011 - 02:24 PM

Hello

Please can someone take some time to read my comments in the below script. Now, I understand how I can use argv. However, I'm having trouble explaining how it actually works. I think it's because there's gaps in my terminology. I was wondering whether someone can tell me if the comments are correct ans show examples of how I could of improved my description. Yes, I'm a n00b. Thank you for your time.

from sys import argv # argument variable allows python to unpack variables.
script, user_name = argv # the user will need to type a variable for the script to run
prompt = '>'
print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions"
print "Do you like me %s" % user_name
likes= raw_input(prompt)
print "Where do you live %s?" % user_name
lives = raw_input(prompt)
print "What kind of computer do you have?"
computer = raw_input(prompt)
print """
Alright so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice
""" % (likes, lives, computer)

Replies To: Problem Explaining 'argv'

Re: Problem Explaining 'argv'

Posted 25 July 2011 - 02:51 PM

Looks like someone is learning the hard way Nice.

Personally, this is what would satisfy me:

# Import the argv module so that we can get
# arguments from the command line.
from sys import argv
# script = argv[0], user_name = argv[1]
# argv[0] is the name of the script and
# argv[1] is the first argument given,
# in this case, the user's name.
script, user_name = argv
prompt = '> '
print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)
print "Where do you live %s?" % user_name
lives = raw_input(prompt)
print "What kind of computer do you have?"
computer = raw_input(prompt)
print """
Alright so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)

Re: Problem Explaining 'argv'

Posted 29 August 2011 - 06:35 PM

Here is how argv works... more or less...

Fake Console#!/!#:mount -t ntfs-3g /dev/sda1 /mnt
--------------Argv is a list!----------------
['mount', '-t', 'ntfs-3g', '/dev/sda1', '/mnt'] is <type 'list'>
--------------Argv is a list!----------------
-------------Here are its elements-----------
mount
-t
ntfs-3g
/dev/sda1
/mnt
-------------Here are its elements-----------
------------This is how to parse them in 30 seconds of code---------------
Mounted /dev/sda1 to /mnt as type ntfs-3g
------------This is how to parse them in 30 seconds of code---------------