繁体   English   中英

为什么以下代码显示错误?

[英]Why does the following code shows an error?

每当我运行以下代码时,它将引发错误。

from sys import argv
 script, first, second, third = argv 
 print "The script is called:", scrip
 print "Your first variable is:", first
 print "Your second variable is:", second
 print "Your third variable is:", third

错误出现-

File "/Users/rishabh/Documents/rishabh.py", line 3
    script, first, second, third ,fourth= argv 
    ^
IndentationError: unexpected indent

解:

from sys import argv
script, first, second, third = argv 
print "The script is called:", scrip
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

只需复制并运行。 它应该工作:p

正如丹尼尔(Daniel)所说:“因为您已经缩进了,所以不应该吗?”

Python对缩进很敏感。 每个缩进都必须是相同数量的单位,无论是单个缩进还是两个空格缩进,甚至是(如果需要)30个制表符!

在您的代码中,每条打印语句行上都有一个(最可能)偶然的空间。 因为它们不需要缩进(如果它们在if语句中,或者它们会进行某种排序),则if会引发错误。 解释器根本无法理解为什么要缩进。

正确的代码(空格已删除):

from sys import argv
script, first, second, third = argv 
print "The script is called:", scrip
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

“脚本”一词前有一个空格(缩进)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM