Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first function
i wrote the following code:

while True:
    howMany = int(input("how many numbers do you want ?"))

    def fibo():
        a = 0
        b = 1
        count = 0

        while count < howMany:
            c = a + b
            a = b
            b = c
            count += 1

    number_exe = int(input("choose a number bellow or above 5: "))

    if number_exe > 5:
        print("number is smaller than 5, fibo() function didn't execute")
i don't know if there had to be a function written for it, but there's my first function :)
It is a function, sort of, but it doesn't do anything. It doesn't produce a result.

Do not define a function inside a while loop. It is not an error to do so, but it is not where I would expect to find a function definition. Functions are usually defined near the top of a module. fibo() should appear above "while True:" in the file.

Don't use global variables to pass information to a function. Use function arguments.

Your function doesn't return a value. It calculates numbers, but nothing is done, or can be done, with those numbers. I would write this function as a generator, but maybe you should start out with returning the last number in the sequence or a list of the numbers.

Why are you limiting howMany to be > 5?
fibo(0) : None? Empty list?
fibo(1) : 1
fibo(2) : 1 1
fibo(3) : 1 1 2
fibo(4) : 1 1 2 3

Why are you using a while loop to compute the numbers? You know how many times the loop needs to run. Use "for _ in range(howMany):". Let python do the loop counting for you.

Why are there 3 variables, a, b, c? I know you are using c as a temporary swap variable to hold the sum of a+b, but you don't need swap variables in Python. In python you can do this:
a, b = b, a+b
which does the same as:
c = a + b
a = b
b = c
okay, thank you very much

and btw, Dean, i just wanted to say that although i said there's truth to what you said (in the 'prime numbers' thread) - one thing is not - and that's that stuff goes in one ear and through the other,
i do comprehend what you (and others) are saying, it's just that i need to read more on certain subjects and basically just dig dipper...

and i appreciate your help immensely...
and regarding the question in relation to howMany > 5 ... - it's not 'howMany' - it's 'number_exe' - and i did it just to see if i can call the function from within a condition...
Why wouldn't you be able to call a function inside a conditional? You must be doing that in other code you've written (whether your own functions or others).
i don't know hehe...i guess i just don't take anything for granted
btw, the reason i wrote the function within a while True loop is because i wanted it to keep there a different way to make it (repeating) ?
You don't need to define the function every time in the loop, at least not if you pass arguments to it rather than making it a closure as you have done.
ndc, what do you mean making it a closure ?
Quote:btw, the reason i wrote the function within a while True loop is because i wanted it to keep there a different way to make it (repeating) ?
That is not how functions work. A function executes each time it is called. For example, the function fibo() never executes in this code:
for _ in range(1000000):

    def fibo(count):
Run the program and it doesn't print anything. The code inside fibo() never executes because there is no code that calls the function.

In this example fibo() is called 10 times
def fibo(count):

for i in range(10):
It is difficult to believe that you did any research on how to write a function. I googed "how to write a python function", read the first 10 responses (did not watch videos), and every one contains this information in some form:
Quote:A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.
Your function example does none of these.
Fun with numbers:

import random

def myApp():    
    one = random.randint(1, 100)
    two = random.randint(1, 100)
    print('first number is', one, 'second number is', two)
    myfibo = [one, two]
    for n in range(2, 10):
        num = myfibo[n-2] + myfibo[n-1]

    result1 = sum(myfibo)    
    result2 = myfibo[6] * 11
    print('The sum of myfibo is', result1)
    print('myfibo[6] x 11 is', result2)
Don't know it that if that function is so helpful,or show what functions is about Pedroski55🧐
(Nov-12-2022, 06:49 PM)astral_travel Wrote: i don't know if there had to be a function written for it, but there's my first function :)
Can take a little basic about functions stuff using Fibonacci sequence.
Usally a functions should return or yield a result out.
def fibo(n):
    a, b = 0, 1
    for _ in range(1, n):
        a, b = b, a + b
    return b
Usage test:
>>> fibo(10)
>>> fibo(15)
>>> [fibo(i) for i in range(1, 11)]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> [fibo(i) for i in range(50, 55)]
[12586269025, 20365011074, 32951280099, 53316291173, 86267571272]
Now let say i want to save this list that fibo 50-55 make.
Then i would not add code for that in fibo() function.
A function should ussally only do one task,this make code easier to read and test.
So to give a eample of this,see that return of fibo function is pass as argument to save_fib.
import json

def fibo(n):
    a, b = 0, 1
    for _ in range(1, n):
        a, b = b, a + b
    return b

def save_fib(file_name, fib_seq):
    with open(file_name, "w") as fp:
        json.dump(fib_seq, fp)

def read_fib(file_name):
    with open(file_name) as jp:
        saved_fib = json.load(jp)
    print(f'The fibo seq saves is:\n{saved_fib}')

if __name__ == '__main__':
    file_name = 'fib_seq.json' 
    fib_seq = [fibo(i) for i in range(50, 55)]
    save_fib(file_name, fib_seq)    
The fibo seq saves is: [12586269025, 20365011074, 32951280099, 53316291173, 86267571272]

Forum Jump:

User Panel Messages

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