我已经在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天前开始的,终于被卡住了。任何帮助将不胜感激。
答案 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.