简体   繁体   中英

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

As Daniel said, "Because you've got an indent, and you shouldn't?"

Python is sensitive to indents. 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!

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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