+ 2
How to report a error in a quiz?
In the Python course, function section, there is a quiz that asks to calculate the Fibonacci number of 4. The answer according to many widely available calculators is 3, but the quiz takes 5 as the correct answer. The function written in the quiz has the Fibonacci of zero returning 1 and it should return zero. This is the bad code in the quiz: def fib(x): if x == 0 or x == 1: return 1 else: return fib(x - 1) + fib(x - 2) print(fib(4)) It should look something like this: def fib(n): if n==1 or n==2: return 1 return fib(n-1)+fib(n-2) print(fib(4))
5 Antworten
+ 5
5
+ 1
What is the result of this code?
def fib(x):
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)
print(fib(4))
out put:5
0
result --------------> 5
- 1
These are functionally the same. I agree that it looks different than what you would traditionally see but it works the same other than including zero as a valid input.
- 2
@James Durand They are not functionally the same, they return 2 different answers, one 3 and the other 5. The point was not to compare the 2 pieces of code, but to point out the error. The correct answer is 3 and not 5.