2016-10-04 53 views
-9

你好,我是新手到Python,我试图去学习,这是我继续打,当我执行下面的代码,哪里是错误缩进错误的基本程序

#!/usr/bin/python 

def main(): 
num1=input("Enter the 1st #\t\t") 
print "First # is\t:", num1 
print 
num2=input("Enter the 2nd #\t\t") 
print "Second # is\t:",num2 
print 
num3=input("Enter the 3rd #\t\t") 
print "3rd #is:,\t",num3 
if(num1>num2) and (num1>num3): 
    print"Highest # is:\t",num1 
elif(num2>num3) and (num2 >num1): 
    print"Highest # is:\t",num2 
else: 
    print "The WINNER IS\n" 
    print num3 

main() 

错误:

python 1.py 
File "1.py", line 4 
num1=input("Enter the 1st #\t\t") 
^
IndentationError: expected an indented block 

我缺少的缩进位置在哪里?

+5

几乎所有的它,目前。你的问题没有缩进来将任何代码放在'def main()'块/作用域内。如果这是你的代码的精确表示,你需要将你打算成为'main'的一部分缩进4个空格 – roganjosh

+0

@raganjosh你是什么意思,你能纠正上面的语法吗? – ady6831983

+3

@ ady6831983当然,任何Python书籍/教程都会解释函数定义中的任何代码都需要缩进。 –

回答

2

您应该使用空格或制表符缩进主函数。 (4位被建议报告)

像这样:

def main() 
    num=input() 
    # rest of your main code 

main() 

我看见你媒体链接这样做是为了if/else语句,你也应该这样做的功能。

我推荐你参加一个像codecademy一样的初学者python课程。

2

您将作出缩进主功能,我在这里重写代码:

Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.

你可以使用Python文档的。了解关于Python的更多信息,以及使用ATOM或PyCharm等IDE来实现更好的编码。

def main(): 
    num1=input("Enter the 1st #\t\t") 
    print "First # is\t:", num1 
    print 
    num2=input("Enter the 2nd #\t\t") 
    print "Second # is\t:",num2 
    print 
    num3=input("Enter the 3rd #\t\t") 
    print "3rd #is:,\t",num3 
    if(num1>num2) and (num1>num3): 
     print"Highest # is:\t",num1 
    elif(num2>num3) and (num2 >num1): 
     print"Highest # is:\t",num2 
    else: 
     print "The WINNER IS\n" 
     print num3 

main() 
1

下的所有代码的DEF的main():应缩进除了线,当你调用main()

2

缩进的代码。

在Python中,你总是有一个冒号后缩进代码(:)否则不知道什么顺序来执行它后高清的main() 就缩进一切: