Python Forum
Made my First function.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Made my First function.
#1
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.
Gribouillis write Aug-28-2022, 10:38 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
(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
Reply
#3
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()).
buran likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
Try turning this into a generator. It is a good fit for a generator and knowing how generators work will be useful.
buran likes this post
Reply
#5
Why the single letter variable names? Why the magic number of 5000?
buran likes this post
Reply
#6
(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
Reply
#7
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
Reply
#8
(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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020