user466534
user466534

Reputation:

writing simple python function

u am starting to learn python, and want to get some experience with functions , for instance i have wrote following simple code in python

def fibonacci(n):
    if n == 1:
        return 1
    elif n == 2:
        return 1
    elif n > 2:
        return fibonacci(n-1)+fibonacci(n-2)

    for n in range(1, 4):
        print(n,", ",fibonacci(n))

but when i have run this code, i am getting just this line

C:\Users\Dato\Desktop\Python_codes\venv\Scripts\python.exe C:/Users/Dato/Desktop/Python_codes/fibonacci.py

Process finished with exit code 0

so why does not it shows me result?

Upvotes: 0

Views: 70

Answers (3)

Alex Skiba
Alex Skiba

Reputation: 19

Im not sure on how you call the function, could you provide more information about that?

import random

any_number = randint()
print(fibonachi(any_number))

Upvotes: 0

user3614104
user3614104

Reputation: 60

The issue you're having is that you never call the function fibonacci.

I think you've got your tabbing off,

    for n in range(1, 4):
        print(n,", ",fibonacci(n))

shouldn't be inside the function.

Try this:

def fibonacci(n):
    if n == 1:
        return 1
    elif n == 2:
        return 1
    elif n > 2:
        return fibonacci(n-1)+fibonacci(n-2)

for n in range(1, 4):
    print(n,", ",fibonacci(n))

Upvotes: 1

Skapin
Skapin

Reputation: 318

you miss the callign part. You just defined a function.

Now, you need to call it.

add

fibonacci(42)

at the end of your code

Upvotes: 1

Related Questions