如何修复:"预期缩进块"错误

每次运行代码时,我都会得到相同的"预期缩进块"。

选择 | 换行 | 行号
  1. from myro import*
  2.  
  3. init("simulator")
  4.  
  5. def main():
  6.  
  7. for x in range(10): #repeats code in loop 10 times
  8.  
  9. forward(1,1) # go forward for a small distance
  10.  
  11. backward(1,1) # go backwards the same distance as forward
  12.  
  13. turnLeft(50,30) # slight turn to vacuum a new area 
  14.  
  15. main()
# 回答1

代码中没有缩进。需要缩进来表示不同的代码块。解释器预期在某些语句之后缩进,如果不存在,则引发缩进错误。例子:

选择 | 换行 | 行号
  1. >>> for i in range(10):
  2. ... print i
  3. Traceback (IndentationError: expected an indented block (<interactive input>, line 2)
  4. >>> 
# 回答2

您忘记缩进包装在"对于"循环。

选择 | 换行 | 行号
  1. from myro import* 
  2. init("simulator") 
  3. def main(): 
  4. for x in range(10): #repeats code in loop 10 times 
  5.   forward(1,1) # go forward for a small distance 
  6.   backward(1,1) # go backwards the same distance as forward 
  7.   turnLeft(50,30) # slight turn to vacuum a new area  
  8. main() 

标签: python

添加新评论