Indentation error basic program
你好,我是Python初学者,正在努力学习,这是我在执行下面的代码时经常碰到的问题,错误在哪里?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #!/usr/bin/python def main(): num1=input("Enter the 1st #\t\t") print"First # is\t:", num1 num2=input("Enter the 2nd #\t\t") print"Second # is\t:",num2 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 " print num3 main() |
错误:
1 2 3 4 5 | python 1.py File"1.py", line 4 num1=input("Enter the 1st #\t\t") ^ IndentationError: expected an indented block |
我丢失的压痕在哪里?
缩进代码。
在Python中,总是必须在冒号(:)后面缩进代码,否则它不知道执行它的顺序。只需在def main()之后缩进所有内容:
您将对主函数进行缩进,我在这里重写您的代码:
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-doc。了解更多关于python的信息,以及使用诸如atom或pycharm之类的IDE来进行更好的编码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def main(): num1=input("Enter the 1st #\t\t") print"First # is\t:", num1 num2=input("Enter the 2nd #\t\t") print"Second # is\t:",num2 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 " print num3 main() |
您应该用空格或制表符缩进您的主函数。(建议4个空格)
这样地:
1 2 3 4 5 | def main() num=input() # rest of your main code main() |
我看到你已经为if/else做好了准备,你也应该为函数做好准备。
我建议你学习一门初级的python课程,比如te one of codecademy。
def main():下的所有代码都应该缩进,但调用main()时的行除外。