#Python 3.0
from sys import argv
script, user_name = argv
prompt = "> "
print("Hi %s, I'm the %s script." % (user_name,script))
print("I would like to ask you a few questions.")
print("Do you like me %s?" % user_name)
likes = input(prompt)
print("Where do you live %s?" % user_name)
lives = input(prompt)
print("What kind of computer do you have?")
computer = 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)
)
我收到以下错误:
script, user_name = argv
ValueError: need more than 1 value to unpack
请帮忙
答案 0 :(得分:1)
尝试
len(argv)
查看argv
有多少元素。您的问题是argv中的元素数量与您尝试将其分配给的变量数量不匹配。
以下是您的问题的简化案例:
>>> one, two = [1] # cannot do this!
ValueError: need more than 1 value to unpack
>>> one, two = [1, 2] # this is OK!
要解决此问题,请确保将参数传递给脚本
python your_script.py user_name
答案 1 :(得分:0)
您需要为程序指定一个参数。如果您没有收到错误,因为argv只包含一个参数,而且它是您程序的名称。
答案 2 :(得分:0)
让我们来看一个较小的程序:
from sys import argv
print(argv)
让我们运行这个:
[7:30pm][wlynch@watermelon /tmp] python3 argv.py abc def ghi
['argv.py', 'abc', 'def', 'ghi']
因此,在您的用例中,您需要将一个参数传递给脚本。