"import sys import argv"问题



我已经一遍又一遍地阅读了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:UsersRich MazelDesktopPythonlpthw> 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 天前开始,终于卡住了。任何帮助将不胜感激。

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

从你的外壳中调用python test15.py some_username至少应该让你通过开箱ValueError

附言考虑使用argparse而不是sys.argv

所以这意味着你没有两个值来表示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.

最新更新