4

我的代码是:

90              if needinexam > 100:
91                  print "We are sorry, but you are unable to get an A* in this class."
92              else:
93                  print "You need " + final + "% in your Unit 3 controlled assessment to get an A*"
94          
95          elif unit2Done == "n":
96              
97          else:
98              print "Sorry. That's not a valid answer."

错误是:

line 97
    else:
       ^
IndentationError: expected an indented block

我完全不知道为什么会出现此错误,但也许你们可以帮助我。它周围有很多 if/else/elif 语句,这可能会让我感到困惑。非常感谢任何帮助,谢谢!

编辑:在 elif 语句中添加“pass”或代码只会将相同的错误移动到第 95 行。

4

2 回答 2

14

尝试这个:

elif unit2Done == "n":
    pass # this is used as a placeholder, so the condition's body isn't empty
else:
    print "Sorry. That's not a valid answer."

这里的问题是unit2Done == "n"条件的主体不能为空。在这种情况下,pass必须将 a 用作一种占位符。

于 2013-06-17T19:03:38.777 回答
1

除了 requires pass,它就在这里:

95          elif unit2Done == "n":  # space space tab tab
96              pass # space space space space tab tab tab
97          else: # space space space space tab tab
98              print "Sorry. That's not a valid answer." # space space tab tab tab

你确实有混合物。

在 vim 中,你可以这样做:

set listchars=tab:>-,trail:.

这就是您的代码的样子:

95  >--->---elif unit2Done == "n":
96    >->--->---pass
97    >->---else:
98  >--->--->---print "Sorry. That's not a valid answer."
于 2013-06-17T19:23:51.013 回答