sum = 0 # 1、以循环计算 1*1+2*2+3*3+...+n*n n = 5 for i in range(1, n + 1): sum += i * i print("循环后的结果:" + str(sum)) # 2、以递归计算1*1+2*2+3*3+4*4+...+n*n def jishuna(k): if k == 0: return 0 else: return k * k + jishuna(k - 1) print("递归的结果:" + str(jishuna(n)))