2015-07-12 92 views
-4
1 print("hello world") 
    2 myname = input("what is your name?") 
    3 print(myname) 
    4 if(myname == "ofir"): 
    5 print("you are great") 

这是我用Python编写的代码(我今天才刚刚开始)。我得到这个错误“预期的缩进块”中的第5行,我不知道我的错。 谢谢你的帮助。我使用3.4.3 python。错误与打印或如果蟒蛇

+4

要多少[Python的教程]的(HTTPS: //docs.python.org/3/tutorial/)你有跟着吗?缩进是该语言的基础*,并在[介绍部分](https://docs.python.org/3/tutorial/introduction.html)中进行了解释。您似乎有* no *缩进,所以Python无法确定'if'块的代码部分开始或结束的位置。 –

+0

我跟着2个python教程和第三个教程的一半。 –

+0

去http://learnpythonthehardway.org/book/我认为这对你更好。 –

回答

2

Python的要求下if语句缩进,以及forwhile循环,functionsclasses等你似乎缺少缩进。我会考虑通过Codecademy互动教程去为Python

print("hello world") 
myname = input("what is your name?") 
print(myname) 
if myname == "ofir": 
    print("you are great") 
3

你应该缩进代码4个空格行,如果它的代码块的一部分。在Python中,这将包括if语句,循环,def和类。我建议你阅读这篇文章:Python-Indetation。要回答您的问题,请将您的代码更改为:

print("hello world") 
myname = input("what is your name?") 
print(myname) 
if(myname == "ofir"): 
    print("you are great") # note the 4 spaces  
0

您需要在if语句后缩进。

并取消代码前三行的缩进。

0

在C编程语言中,一段代码(指令)包含在括号中。缩进无关紧要。在Pascal中,块被包含在'begin''end'中。正如您现在在Python中已经注意到的,“if”,“else”,“for”,“while”,“def”等语句中包含的指令块不包含任何内容。 Python知道如果几个指令具有相同的缩进,则它们是同一个块。

缩进可以是任意数量的空格或制表符,并且可以在Python脚本中变化。唯一的规则是在同一个块内必须是相同的缩进。

请参阅与Python 2.7波纹管检查代码:

a = [1, 2, 3] 
for e in a : 
    if e % 2 == 0 : # indent with 2 spaces 
     print "%d is even" % e   # indent with 4 spaces 
     print 
    else :   # indent with 2 spaces 
    print "%d is odd" % e    # indent with 3 spaces 
    print 
0
1 print("hello world") 
    2 myname = input("what is your name?") 
    3 print(myname) 
    4 if(myname == "ofir"): 
    5  print("you are great") 

您需要缩进print("you are great")为了使程序编译