简体   繁体   English

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

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

Whenever I run the following code, it throws 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

error coming- 错误出现-

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

Solution: 解:

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

Just copy this and run. 只需复制并运行。 It should work :p 它应该工作:p

As Daniel said, "Because you've got an indent, and you shouldn't?" 正如丹尼尔(Daniel)所说:“因为您已经缩进了,所以不应该吗?”

Python is sensitive to indents. Python对缩进很敏感。 Every indent has to be an equal number of unit, whether it be a single tab as an indent two spaces or even (if you wish) 30 tabs! 每个缩进都必须是相同数量的单位,无论是单个缩进还是两个空格缩进,甚至是(如果需要)30个制表符!

In your code you have an (most likely) accidental space on each of the print statement lines. 在您的代码中,每条打印语句行上都有一个(最可能)偶然的空间。 Because they are not in need of indenting (if they were in an if statement or something of the sort they would) if throws an error. 因为它们不需要缩进(如果它们在if语句中,或者它们会进行某种排序),则if会引发错误。 The interpreter simply cannot understand why it should be indented. 解释器根本无法理解为什么要缩进。

The correct code (with removed spaces): 正确的代码(空格已删除):

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