Codecademy Python课程问题

-3 投票
2 回答
641 浏览
提问于 2025-04-18 08:36

我正在通过Codecademy自学Python。不过,当我到达这个部分时,无论我怎么做,系统总是返回同样的错误。有没有其他人遇到过这个问题,或者知道怎么解决?我对编程完全是个新手。
错误的截图可以在这里查看: https://drive.google.com/file/d/0B7fG5IDRoZ3cXzZxbnlpT3RheHc/edit?usp=sharing

answer = 2
def the_flying_circus():
    if ______: answer + 5 = 7
    print "This gets Printed!"
    elif (answer < 5):
        print "As does this!"
    else end

错误信息是:

elif (answer < 5):
   ^
SynxtaxError: invalid syntax 

2 个回答

0
else: end you are missing a `:`

你有很多其他的语法错误:

 if ______: answer + 5 = 7

你不能把值赋给一个运算符。

除非end是某个地方定义的变量,否则使用else: end不是Python的语法。
我不太确定你想要什么,但从你的代码来看,这样写似乎更合理:

answer = 2
def the_flying_circus():
    if answer + 5 == 7:
        print "This gets Printed!"
    elif answer < 5:
        print "As does this!"
    else:
        return answer
4

错误出在你的缩进上。

你不能在没有if块的情况下使用elif。因为你的if语句在一行,而下一个打印语句在下一行,所以你没有满足这个要求。我也不太明白你想在if ________:这一部分做什么。

这里有一个修正的方法:

answer = 2
def the_flying_circus():
    if answer + 5 == 7:
        print "This gets Printed!"
    elif (answer < 5):
        print "As does this!"
    # ...

撰写回答