![]() |
|
Argv help - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Argv help (/Thread-Argv-help) |
Argv help - Alan Turing - 01-02-2014 I'm new to Python, and I'm reading "learnpythonthehardway" and so far I think it's good, but i'm having some trouble with chapter 14 which talks about argvs. My script: PHP Code: from sys import argv
script, user_name = argv
prompt = '> '
print "Hi %r, i'm the %r script, nice to meet you." % (user_name, script)
print "If you don't mind, %r, i'd like to ask you a few questions?" % user_name
print "Do you like me, %r?" % user_name
opinion = raw_input(prompt)
print "Where do you happen to reside, %r?" % user_name
residence = raw_input(prompt)
print "So what operating system do you operate on, %r?" % user_name
OS = raw_input(prompt)
print "And lastly, what race are you?"
race = raw_input(prompt)
print """
So, you said %s when I asked if you liked me.
You live in %s, nice area I guess.
You operate on %s, pretty lame if you ask me.
You are %s, very interesting.
""" % (opinion, residence, OS, race)
But then it asks me "Add another argument to your script" So I assume it's asking me to add another argument in the second line being PHP Code: script, user_name = argv
But I don't know what to put, since i'm having trouble thinking about where I would put it. So could anyone please just add a new arg for me to inspect to understand what I could have done? I added a new arg called nick_name, like so: PHP Code: script, user_name, nick_name = argv
My full script now: PHP Code: from sys import argv
script, user_name, nick_name = argv
prompt = '> '
print "Hi %r, i'm the %r script, nice to meet you." % (user_name, script)
print "If you don't mind, %r, i'd like to ask you a few questions?" % user_name
print "%r, make up a nickname please.?" % user_name
nick_name = raw_input(prompt)
print "%r, your nickname is %r? Wow." % (user_name, nick_name)
print "Do you like me, %r?" % user_name
opinion = raw_input(prompt)
PHP Code: Traceback (most recent call last):
File "ext13.py", line 3, in <module>
script, user_name, nick_name = argv
ValueError: need more than 2 values to unpack
RE: Argv help - Dyme - 01-03-2014 Your initial code is fine, you just need to specify your arguments when you run the script as that's how argv acquires them. See the following: Code: [pens@anus:~]$ python deng.py gay
Hi 'gay', i'm the 'deng.py' script, nice to meet you.
If you don't mind, 'gay', i'd like to ask you a few questions?
Do you like me, 'gay'?
> no
Where do you happen to reside, 'gay'?
> new york
So what operating system do you operate on, 'gay'?
> lunix
And lastly, what race are you?
> snail
So, you said no when I asked if you liked me.
You live in new york, nice area I guess.
You operate on lunix, pretty lame if you ask me.
You are snail, very interesting.deng.py is argv[0] or "script" gay is argv[1] or "user_name" RE: Argv help - Alan Turing - 01-03-2014 (01-03-2014, 12:56 AM)Pens Wrote: Your initial code is fine, you just need to specify your arguments when you run the script as that's how argv acquires them. See the following: So for my second argument, it would go something like? PHP Code: Vulsion~# python test.py dongs rupert(for my nick_name, aka the second argument)?
RE: Argv help - Dyme - 01-03-2014 (01-03-2014, 01:11 AM)VolPlus Wrote: So for my second argument, it would go something like? Yes if you're using the second version of the code you added (which requires 3 args including the filename). RE: Argv help - Alan Turing - 01-03-2014 (01-03-2014, 01:16 AM)Pens Wrote: Yes if you're using the second version of the code you added (which requires 3 args including the filename). Alright, thanks, i'll try it out. |