Python Forum

Full Version: Made my First function.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
(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()
Output:
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
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()).
Try turning this into a generator. It is a good fit for a generator and knowing how generators work will be useful.
Why the single letter variable names? Why the magic number of 5000?
(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. Smile
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
Output:
0 10 10 20 30 50 80 130 210 340 0 1 1 2 3 5 8 13 21 34 First number in Fibonacci sequence >= 500 is Fibonacci[15] = 610
(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)