出现“ import sys import argv”问题

时间:2018-09-26 01:45:25

标签: python

我已经在Python上辛苦地阅读了一遍又一遍的书,无论我做什么或重新阅读和研究了多少书。我找不到任何可以解释此错误的信息:

from sys import argv

script, user_name = argv
prompt = '> '**

print(f"Hi {user_name}?")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
likes = input(prompt)

print(f"Where do you live {user_name}?")
lives = input(prompt)

print(f'What kind of computer do you have?')

computer = input(prompt)

print(f""" 
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
and you have a {computer} computer. Nice.
""")

我不断收到错误:

PS C:\Users\Rich Mazel\Desktop\Python\lpthw> python test15.py
Traceback (most recent call last):
File "test15.py", line 2, in <module> script, user_name = argv 
ValueError: not enough values to unpack (expected 2, got 1)

我实际上是3天前开始的,终于被卡住了。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您正试图将传递给test15.py脚本的args解压缩到script, user_name中。因此,当您调用它时,请确保实际使用用户名 进行调用。

从外壳程序中调用python test15.py some_username至少应使您摆脱拆包ValueError的麻烦。

P.S。考虑使用argparse代替sys.argv

答案 1 :(得分:0)

所以这意味着您没有argv的两个值,所以?

你就是做不到。

除非您在命令行上运行

python file_including_code.py foo
Hi foo.py?
I'd like to ask you a few questions.
Do you like me foo?
> **Yes
Where do you live foo?
> **earth
What kind of computer do you have?
> **Windows

Alright, so you said Yes about liking me.
You live in earth. Not sure where that is.
and you have a Windows computer. Nice.