以下是Zed A. Shaw的“学习蟒蛇的艰难方式”的片段。当我将第一个或第二个变量初始化为argv时,我收到错误。我还没有找到解释。
from sys import argv
script, first, second, third=argv
print "the script is called:",script
print "your first variable is called ",first
print "your second variable is called ",second
print "your third variable is called ",third
答案 0 :(得分:3)
我可以复制错误以查看发生了什么错误吗?
当我执行上面的代码时,它会正常运行。
不使用COMMA
Argv检查空间。
$ python b.py 1 2 3
the script is called: b.py
your first variable is called 1
your second variable is called 2
your third variable is called 3
<强>版本强>
⚡ root@dev # uname -a
Linux dev 4.4.0-1031-aws #40-Ubuntu SMP Thu Aug 10 11:29:58 UTC 2017 x86_64
x86_64 x86_64 GNU/Linux
⚡ root@dev # cat /etc/issue
Ubuntu 16.04.2 LTS \n \l
⚡ root@dev # python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
答案 1 :(得分:0)
您没有初始化第三个参数。你正在拆包argv。 a,b,c = argv
就像a = argv[0]; b = argv[1]; c = argv[2];...
答案 2 :(得分:0)
如果要在参数之间使用逗号,则始终需要提供一个空格,否则编译器会认为def is_palindrome(seq):
n = len(seq)
m = n // 2
q = m + n % 2 - 1
return seq[:m] == seq[:q:-1]
print(is_palindrome('ciao'))
# False
print(is_palindrome('aboba'))
# True
print(is_palindrome('abooba'))
# True
是一个参数,因此会出现错误:
1,2,3
像这样调用脚本:
ValueError: need more than 2 values to unpack