Home > other >  Python recursion sum 1-100 and a bigwig problem where beginners
Python recursion sum 1-100 and a bigwig problem where beginners

Time:11-03

CodePudding user response:

1. The problem of indentation
2. The global and local variables need to know about the knowledge

CodePudding user response:

On the second floor right
1, the indentation you have one more tabs
2, your function is can't find the num of the variable, you want inside the function definition num
A simple for loop is
 
Def func (n) :
Num=0
For I in range (n + 1) :
Num=num + I
Print (num)

Func (100)

CodePudding user response:

Inside the function add
Global num

CodePudding user response:

 def func (n) : 
If n==0:
Return 0
The else:
Return the n + func (n - 1)

Print (func (10))

CodePudding user response:

You have to make clear the scope of the function variables, function outside the num of variable and function inside the num is different

CodePudding user response:

Such use variables, is not a good habit
  • Related