Affichage de la variable dans la fenêtre graphique à partir de la fonction Python 3.4

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

  •  23-12-2019
  •  | 
  •  

Question

J'ai trouvé et modifié du code pour générer le nième de la séquence de Fibonacci, mais mon problème est de l'afficher dans une fenêtre graphique.

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

Il l'imprime et affiche dans la fenêtre que le terme de Fibonacci pour N est "Aucun".Le numéro imprimé est correct.Comment puis-je modifier cela pour afficher le numéro imprimé ?

Était-ce utile?

La solution

Votre fonction fibN ne renvoie rien :

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

Pour la sortie de fonction (le nombre fib) pour remplacer l'appel, vous devez 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

Alors tu peux faire print(fibN(something)) si vous le souhaitez, mais vous pouvez également l'utiliser dans des graphiques comme vous le faites actuellement.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top