Python——递归
#实现阶乘运算 def factorial(n): if n==1: return 1 else: return n*factorial(n-1) m = input("请输入您要计算阶乘的数字:") if m.isdigit(): m=int(m) print(factorial(m)) else: print("invalid input") #实现斐波那契数列 #通向示:f(n)=f(n-1)+f(n-2)//(n=1,n=2时输出1) def fabnic(n): if n<1 : return 1 elif n==1 or n==2:#n==1|n==2 与n==1 or n==2不等价 return 1 else : return fabnic(n-1)+fabnic(n-2) m_1=input("Please enter one number:") if m_1.isdigit(): m_1=int(m_1) print(fabnic(m_1)) else: print("invalid input") #递归计算幂运算 def power(x,n): if n==0: return 1 else : return x*power(x,n-1) m_2=input("请输入您要计算的幂运算的数字:") m_2=m_2.split() x_0=m_2[0] n_0=m_2[1] if x_0.isdigit() and n_0.isdigit() : x_0=int(x_0) n_0=int(n_0) print (power(x_0,n_0)) else : print ("invalid input")