Made my First function. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code Review (https://python-forum.io/forum-46.html) +--- Thread: Made my First function. (/thread-38055.html) |
Made my First function. - PythonBorg - Aug-28-2022 def my_fibonacci(): p = 0 c = 1 result = (p + c) print(result,"",end = "") while result < 5000: p = c c = result result = p + c print(result,"",end = "") my_fibonacci()Hello everybody, Made my first function today. Had a look at other functions on this subject and got lost with the code, it looked so complex.... so i thought to myself, there has to be a simple way of doing this. So out came the pen & paper. I wrote the Fibonacci formula down and ask myself , now how do i put this into python code...? I believe it took me about an hour or so to come up with the function above. hope it display correctly. Am pleased with myself... What would you do differently, but at the same time keeping it as simple as possible. At this time , I do not care if it runs slow or fast. As long as I can understand the code. Rdgs. RE: Made my First function. - Gribouillis - Aug-28-2022 (Aug-28-2022, 10:33 AM)PythonBorg Wrote: What would you do differently, but at the same time keeping it as simple as possible.This isn't bad for a first attempt. Here is a variation on the same algorithm def my_fibonacci(): p, c = 1, 1 while p < 5000: p, c = c, p + c print(p, end=' ') print() my_fibonacci()
RE: Made my First function. - rob101 - Aug-28-2022 Cool. How about modding your function, so that you pass to it two numbers: a start and a stop. Then, have your function return the sequence, which the main body of the script will display (or print() ).
RE: Made my First function. - deanhystad - Aug-28-2022 Try turning this into a generator. It is a good fit for a generator and knowing how generators work will be useful. RE: Made my First function. - ndc85430 - Aug-29-2022 Why the single letter variable names? Why the magic number of 5000? RE: Made my First function. - PythonBorg - Sep-03-2022 (Aug-29-2022, 05:55 AM)ndc85430 Wrote: Why the single letter variable names? Why the magic number of 5000? Single Letter names keeps it nice and Simple..... 5000 keeps it rolling of the screen, allowed me to see what was happening. RE: Made my First function. - deanhystad - Sep-03-2022 Meaningless variable names do not make anything simple. With no documentation other than code, you are forced to read the entire module to understand what it does. That may be fine if it is only one short function, but even there it slows me down. Always write code thinking about how it will be reused and maintained. If you don't, the future you will be very unhappy with the lazy current you. Even something simple like the Fibonacci sequence is complex enough to benefit from documentation. from typing import Tuple # Using Python < 3.9 def fibonocci(count:int=None, start:Tuple[float, float]=(0, 1)) -> float: """Iterator returns next value in the Fibonacci sequence. The Fibonacci sequence is a series where each number is the sum of the two that precede it. count: Number of values in sequence. Default is None for infinite sequence. start: Starting values of the sequence. Usually 0, 1 """ previous, current = start if count is None: end = 3 increment = 0 else: end = count increment = 1 if end > 0: yield previous if end > 1: yield current counter = 2 while counter < end: previous, current = current, previous + current yield current counter += increment print(*fibonocci(count=10, start=(0, 10))) print(*fibonocci(10)) for index, value in enumerate(fibonocci()): if value > 500: print(f"First number in Fibonacci sequence >= 500 is Fibonacci[{index}] = {value}") break
RE: Made my First function. - buran - Sep-20-2022 (Sep-03-2022, 08:44 AM)PythonBorg Wrote: Single Letter names keeps it nice and Simple.....Absolutely wrong. (Sep-03-2022, 08:44 AM)PythonBorg Wrote: 5000 keeps it rolling of the screen, allowed me to see what was happening. Magic number (programming) |