PyScripter导入ARGV

时间:2014-12-10 10:20:38

标签: python argv

不太确定如何让以下代码在PyScripter中正常工作。

from sys import argv

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 question."
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 the fuck that is!
And you have a %r computer. It's obviously a piece of shit!
""" % (likes, lives, computer)

感谢。

1 个答案:

答案 0 :(得分:0)

在解压缩argv到2变量时,您需要在终端中传递user_name

from sys import argv
script, user_name = argv

print user_name

演示:

/Desktop$ python p.py userrrr
userrrr

但如果您在运行时没有传递用户名,则会收到该错误:

~/Desktop$ python p.py 
Traceback (most recent call last):
  File "p.py", line 51, in <module>
    script, user_name = argv
ValueError: need more than 1 value to unpack