Fibonacci Python ā€” unexpected additional output ā€œNoneā€, in the end | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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