Codeacademy上的简单递归

hea*_*her -1 python math recursion

目前在codecademy上做一个递归教程.它告诉我,我是对的,可以进入下一课,但也提出:

File "python", line 6
    else:
       ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

显然这是不对的!你能看看这个链接并告诉我什么了吗?!我很困惑!

http://www.codecademy.com/courses/python-intermediate-en-7f7dx/0/2#

如果我的代码没有出现,它是:

def timesTwo(n):
    if(n == 0):
        return '2 x 0 = 0'

    n*2
    else: 
        return timesTwo(n-1) + 2 
Run Code Online (Sandbox Code Playgroud)

多谢你们

小智 5

以下内容适合您.

def timesTwo(n):
    if(n == 0):
        return 0 # What is 2 x 0 again? I forgot... 
    else: 
        return timesTwo(n-1) + 2  #You write this! 
Run Code Online (Sandbox Code Playgroud)

我也不确定你n * 2回来后为什么这么做.这是一个语法错误,因为你if错过了它else.