我正在努力学习python,我正在做第一个argv练习,并获得一个

2024-09-28 23:19:42 发布

您现在位置:Python中文网/ 问答频道 /正文

在艰苦学习python练习13中,我们第一次导入了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

我得到的结果是:

donny@donny:~/Documents/pygame-scripts$ python ex13.py first second third
Traceback (most recent call last):
  File "ex13.py", line 1, in <module>
    d
NameError: name 'd' is not defined

我想不通。我在谷歌上搜索了一下,确认我完全复制了代码。任何帮助都将不胜感激。你知道吗


Tags: 代码frompyyourissysscriptvariable
2条回答

问题是print的参数之间缺少逗号。你知道吗

请尝试以下操作:

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

编辑:

要使代码与python3兼容,请使用print(arg1, arg2)而不是print arg1, arg2。你知道吗

字符串后需要,。如下所示

print 'The script is called:', script
print 'Your first variable is:', first
print 'Your second variable is:', second
print 'Your third variable is:', third

相关问题 更多 >