在CodingBat上解决雪茄和松鼠拼图会引发语法错误

mag*_*tar 1 python if-statement syntax-error

我试图解决这个 CodingBat问题:

喜欢聚会的松鼠聚在一起抽雪茄.只有当工作日的雪茄数量在40到60之间时,这样的一方才被认为是成功的.然而,在周末,雪茄的数量没有上限.编写一个函数,如果具有给定值的一方成功,则返回True.

不幸的是,虽然偶尔使用Python,但我还不够理解为什么我的代码在第5行出现语法错误而失败:

def cigar_party(cigars, is_weekend):
  if is_weekend:
    if cigars >= 40:
      return True
  else if:
    cigars >= 40 and cigars =< 60:
      return True
  else:
    return False
Run Code Online (Sandbox Code Playgroud)

tcd*_*ney 5

在Python中,您需要使用elif而不是else if.

更多信息:http: //docs.python.org/2/tutorial/controlflow.html

同时更改以下行:

else if:
cigars >= 40 and cigars =< 60:
Run Code Online (Sandbox Code Playgroud)

对此:

elif cigars >= 40 and cigars <= 60:
    return True
Run Code Online (Sandbox Code Playgroud)

需要小于或等于符号,<=并且关键字elif和表达式的其余部分之间不应该有冒号.