0
Can someone explain this, def fib(x): if x==1 or x==0 return 1 else: return fib(x-1) + fib(x-2)
https://code.sololearn.com/WnyG5bX7FIMv/?ref=app https://www.sololearn.com/discuss/2881979/?ref=app
2 Antworten
+ 3
Izizi Abdul ,
▪︎please write the code NOT in the headline. write it in the text area and use proper formatting.
▪︎the code is a python function. it looks like that the it calculates a fibonacci series
▪︎please do not attach unrelated links
+ 1
def fib(n):
//base case
if n == 0:
return 0
elif n == 1:
return 1
//operation
else:
return fib(n-1) + fib(n-2)
1. First we define a function
2. Then we create the base case. What happens if we insert 0 or 1? Let's handle this.
3. Then comes the main operation of the fibonacci sequence.
The reason why people oftentimes create the fibbonacci sequence as a function is that it's perfect to learn about recursion. The function calls itself within the function. Try a large number as the input. The calculation will take forever.