首先,我要说的是我对编程完全陌生。我最近刚刚开始使用Python,它一直在用一个重复出现的错误来踢我的头--“预期的缩进块”。现在,我知道有其他几个线程解决了这个问题,我已经检查了很多线程,然而,即使检查我的缩进也没有给我带来更好的结果。我已经用4个空格替换了所有的缩进,甚至重写了几次代码。我将把我得到的这个计数器赋值作为例子。
option == 1
while option != 0:
print "MENU"
option = input()
print "please make a selection"
print "1. count"
print "0. quit"
if option == 1:
while option != 0:
print "1. count up"
print "2. count down"
print "0. go back"
if option == 1:
print "please enter a number"
for x in range(1, x, 1):
print x
elif option == 2:
print "please enter a number"
for x in range(x, 1, 1):
elif option == 0:
break
else:
print "invalid command"
elif option == 0:
break
发布于 2015-01-20 00:53:37
在python中,.....intendation很重要,例如:
if a==1:
print("hey")
if a==2:
print("bye")
print("all the best")
在本例中,如果执行这两个条件中的任何一个,都将打印"all the best“,但如果是这样
if a==2:
print("bye")
print("all the best")
则仅当为a==2时才会打印"all the best“
发布于 2011-08-02 00:27:58
您的for
循环没有循环体:
elif option == 2:
print "please enter a number"
for x in range(x, 1, 1):
elif option == 0:
实际上,整个if option == 1:
块都有缩进问题。elif option == 2:
应与if
语句处于同一级别。
发布于 2011-08-02 00:28:18
您的最后一条for
语句缺少一个正文。
Python希望缩进块跟在带有for的行之后,或者在冒号后面有内容。
第一种样式更常见,所以它说它希望后面跟着一些缩进的代码。您有一个缩进级别相同的elif
。
https://stackoverflow.com/questions/6901436
复制相似问题