Visualización de variables en la ventana de gráficos de la función Python 3.4

StackOverflow https://stackoverflow.com//questions/25065897

  •  23-12-2019
  •  | 
  •  

Pregunta

Encontré y edité algo de código para generar la enésima secuencia de Fibonacci, pero mi problema es mostrarlo en una ventana gráfica.

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()

Lo imprime y muestra en la ventana que el término de Fibonacci para N es "Ninguno".El número que imprime es correcto.¿Cómo puedo cambiar esto para mostrar el número impreso?

¿Fue útil?

Solución

Tu función fibN no devuelve nada:

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

Para la salida de la función (el número fib) para reemplazar la llamada, debe 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

Entonces puedes hacer print(fibN(something)) si quieres, pero también puedes usarlo en gráficos como lo estás haciendo ahora.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top