Python function called twice

Hi i am new to this language, i intentionally called a function twice and to my suprise am getting results i wasn’t expecting: what i see is like loop while loop but it ends up with a message that says the kerrnel is dead: as a learner i want to know what is happening when i called the function with and indented line in the code:

below is my code sample

def fbf():
    i = 0
    while i < 31:
    # for i in range(1,51):
        if i % 3 == 0 and i % 5 == 0:
            print("FizzBuzz")
        elif i % 3 == 0:
            print("Fizz")
        elif i % 5 == 0:
            print("Buzz")
        else:
            print(i)
        i += 1
     fbf()
fbf()

The only time your function is called from the main script, is when the script is run for the first time. After that, as soon as i == 31, the function calls itself, creating an infinite loop.

In fact, almost everything in the while: loop is simply noise; you can can do it like this:

def fbf():
    i = 0
    while i < 31:
        i += 1
    fbf()

Maybe a better illustration:

def fbf(called_from):
    print(f"fbf called from {called_from}")
    i = 0
    while i < 31:
        i += 1
    fbf("fbf")


fbf("main")

To add: I don’t ‘know’ because I’m not a Python core developer, but I would assume that there has to be some kind of a ‘return’ stack or pointer, that is being incremented with each call. As there is no return, said will simply grow in size until something (maybe a buffer) becomes overwhelmed and the system fails.

I guess you did not mean to call fbf() inside of fbf. Remove that call at the end the fbf function.

fbf calls fbf calls fbf cals fbf … until python runs out of stack and raises the RecursionError exception.
That will be after about 1,000 calls.

Thank you Rob, as am learning, may i know how you would fit the fizbuzz into your simplified code

Thank you Barry, am happy with your explanation, i actually wanted to know why the loop happened. wont be using the indented call line at all. it was a try to figure out what problems i would encounter.

I don’t think that’s true, is it? fbf will go on fizzing and buzzing [1] until i==31, but then I agree it calls itself, and the whole thing starts again, until the stack overflows.

[1] that noise is intended

Jeff i want to believe thinks my focus is on the function. actually i was really working on the fizzbuzz, it was later i turned it into a function just to test my indentation skill. at first the when i called the function with the line indented i had no results. i outdented it and had the desired results. However i just included the indented portion and discovered the looping and now as a learner i wanted to understand what brought about that. but now i learned it meant the function was ‘calling itself’

am grateful to you all

I meant no disrespect by saying “almost everything in the while: loop is simply noise” – one mans noise, is another mans music. All I meant was that the while: loop is incidental to the infinite loop that was created by the function calling itself, which is the fundamental error here and what I assumed to be the issue.

aww Rob i don’t think i was offended by the statement. i understood what you said. no cause for alarm mate :grinning: :grinning:

1 Like

@JackAmin

Not too sure what your intention was, but if its to call the function x number of times, maybe:

def fbf(calls):
    print(f"call: {calls+1}")
    i = 0
    while i < 31:
        if i % 3 == 0 and i % 5 == 0:
            print("FizzBuzz")
        elif i % 3 == 0:
            print("Fizz")
        elif i % 5 == 0:
            print("Buzz")
        else:
            print(i)
        i += 1
    print()
    return


for call in range(3):
    fbf(call)

… where x is the number in range(3)

Thanks a lot Rob, i have just played around with your code and made sense off it. am grateful, will be around to learn from you all.

1 Like