+ 1

Fibonacci Python — unexpected additional output “None”, in the end

Hi lovely people, how are you todayyyy 💛💙 😄 🐍I’ve got a specific question: This code below almost works — i do get the desired output..but i also get an annoying “None”, as an additional output in the end. And i don’t know how to make it go away. So my output is: 0 1 1 2 3 None If I make the “None” go away, SoloLearn will accept it. And my code is: num = int(input()) def fibonacci(n): #complete the recursive function fibolist = [0, 1] for i in range(n): if i>=2: fibolist.append(fibolist[i-2] + fibolist[i-1]) for el in fibolist: print(el) print(fibonacci(num)) 🤔Any ideas? Please do NOT post your solution in this thread — there are many Fibonacci Python threads in SoloLearn already. This is a specific question, which might help users who have also seen this situation elsewhere. So why is the “None” there? Thank you 🙏😊

21st Feb 2021, 2:23 PM
Christos Nikolis
Christos Nikolis - avatar
7 Antworten
+ 5
Because you are trying to print the value returned by function call "fibonacci(num)" even though you aren't returning anything from function , so it prints None .
21st Feb 2021, 2:26 PM
Abhay
Abhay - avatar
+ 3
Christos Nikolis You are already printing values in loop inside function so don't print function Just call function.
21st Feb 2021, 2:26 PM
A͢J
A͢J - avatar
+ 2
Christos Nikolis Using return actually stops further execution of function and only first value is returned. You can instead use yield. num = int(input()) def fibonacci(n): #complete the recursive function fibolist = [0, 1] for i in range(n): if i>=2: fibolist.append(fibolist[i-2] + fibolist[i-1]) for el in fibolist: yield el for i in fibonacci(num): print(i) I never went into how does yield actually works , so i can't explain it either but in case you wanna know here is a good article about it. https://www.guru99.com/JUMP_LINK__&&__python__&&__JUMP_LINK-yield-return-generator.html#:~:text=Summary%3A-,The%20yield%20keyword%20in%20python%20works%20like%20a%20return%20with,are%20only%20available%20when%20called.
21st Feb 2021, 3:30 PM
Abhay
Abhay - avatar
+ 2
Christos Nikolis Thank you :)
21st Feb 2021, 4:13 PM
Abhay
Abhay - avatar
+ 1
I Am AJ ! — that works, and thank you! The only issue I have with your solution is that that was SoloLearn’s premade code (i mean the final print), and so for the sake of challenge i didn’t want to change it.
21st Feb 2021, 2:41 PM
Christos Nikolis
Christos Nikolis - avatar
+ 1
Abhay — thanks a lot! I have now replaced the print with *return*, at the end of my function: for el in fibolist: return(el) # that was print(el) initially And the “None” issue is solved — but now my output is just a single 0. I know i ask somethind beyond my first question, but any idea why I don’t get returned every element (el) from my fibolist? As part of the challenge, i don’t want to change SoloLearn’s premade code, at the end of the exercise: print(fibonacci(num)) I understand if you don’t have the time to reply again, but i’d love it if you did! (-:
21st Feb 2021, 3:19 PM
Christos Nikolis
Christos Nikolis - avatar
+ 1
Abhay — You have earned yourself a new follower 😁I understand that *return* breaks the function. I will also read your article, and here’s one more from me: https://stackoverflow.com/questions/34968634/how-to-return-values-without-breaking-the-loop?newreg=706108581a88413eaedf8aface1ccdb7
21st Feb 2021, 3:40 PM
Christos Nikolis
Christos Nikolis - avatar