Question

I've found and edited some code to generate the nth of the Fibonacci Sequence, but my issue is with displaying it in a graphics window.

from graphics import *



def main():

    win = GraphWin("Sample Input", 300, 300)

    win.setCoords(0,0, 299, 299) # not required, just convenient



    input_prompt = Text(Point(150, 200),"Enter a positive integer")

    input_prompt.setStyle('bold italic') # not required, just convenient

    input_prompt.draw(win)



    input_integer = Entry(Point(150, 150),5)

    input_integer.setText("    ")

    input_integer.draw(win)



    win.getMouse()



    integer_number = int(input_integer.getText())  # the very line that does the input!



    running_count = 0

    for x in range(1, integer_number + 1):

        running_count = running_count + x

        def factorial(n):
            if n == 1:
                return n
            else:
                return n*factorial(n-1)

        def fibN( k ):
            k = abs( int( k ))
            if k == 0:
                fib = 0
            elif k == 1:
                fib = 1
            else:
                counter = 2  
                f0 = 0
                f1 = 1
                fib = f0 + f1
                while counter <= k:
                    fib = f0 + f1
                    f0 = f1
                    f1 = fib
                    counter += 1
            print(fib)

        fib_result = "Fibonacci", "term", "of", integer_number, "is", fibN(integer_number)

        prompt = Text(Point(150, 50), fib_result)

        prompt.draw(win) 

    result_string = "The number is "+str(running_count)+"."

#     Text objects require a string, not a number

    result = Text(Point(150, 100), result_string)

    result.draw(win)

    factorial_result = "Factorial", "of",integer_number, "is", factorial(integer_number) #brackets displayed, separated factorial, of

    prompt = Text(Point(150, 75), factorial_result)

    prompt.draw(win)      

    prompt = Text(Point(150, 25),"Click to end")

    prompt.draw(win)

    win.getMouse()

    win.close()



main()

It prints it, and it displays in the window that the Fibonacci term for N is "None". The number it prints is correct. How can I change this to display the printed number?

Was it helpful?

Solution

Your fibN function doesn't return anything:

def fibN( k ):
        k = abs( int( k ))
        if k == 0:
            fib = 0
        elif k == 1:
            fib = 1
        else:
            counter = 2  
            f0 = 0
            f1 = 1
            fib = f0 + f1
            while counter <= k:
                fib = f0 + f1
                f0 = f1
                f1 = fib
                counter += 1
        print(fib) #this only puts the output on the screen

For the function output (the number fib) to replace the call, you need to return fib:

 def fibN( k ):
        k = abs( int( k ))
        if k == 0:
            fib = 0
        elif k == 1:
            fib = 1
        else:
            counter = 2  
            f0 = 0
            f1 = 1
            fib = f0 + f1
            while counter <= k:
                fib = f0 + f1
                f0 = f1
                f1 = fib
                counter += 1
        return fib

Then you can do print(fibN(something)) if you want, but you can also use it in graphics like you're doing now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top