Python中的argv函数运行遇到的小问题,总是出现not enough values to unpack(expected2,got1 )这种情况(我用的mac,在windows上没试过)
最近在学Python编程,作为一个编程小白,正在看同学推荐的Learn Python the hard way
里面讲到了一个函数argv
源代码是这样的
// 书上的代码
from sys import argv
script, first, second, third = argv
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:",third)
然后我就把这段代码丢进pycharm里面,然后点击运行,结果就
底下的操作台是这样的
//操作台
Traceback (most recent call last):
File "/Users/haibo/PycharmProjects/untitled/venv/Learn Python the hard way.py", line 35, in <module>
script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 1)
Process finished with exit code 1
当时网上看了很多东西看的稀里糊涂,后来又问了朋友,最后才明白原来argv这个函数是需要在cmd里面运行的,就是这个
然后,正确的做法应该是这样,打开操作台,然后键入python + (文件的路径)+ 键入三个值,中间用空格隔开,就是这样
终于就成功运行了这个破程序,疯了,但是随着后来的练习又有新的问题出现,这里暂时先不聊,等到我解决了再来跟大家分享。希望我能帮助到遇到同样问题的小白😭