2016-05-07 6 views
0

Ich mache ein Stück Code für eine Schule mit 7-10 Jahre alten Studenten, die ihre grundlegenden arithmetischen Fähigkeiten testen werden. Das Programm ist voll funktionsfähig und sollte es dem Lehrer ermöglichen, die Noten später in Textdateien zu sehen. Derzeit sind es 556 Zeilen. Ich habe es so weit wie möglich verkürzt, aber ich kann nicht scheinen, um dieses ein Stück Code zu arbeiten:Während Schleife in Funktion nicht funktioniert (Python 3.5.0)

def Repeat(repeat_q, option_right, t_class_right): 
    repeat_q = input("\n\nWould you like to see any other class details? ") 
    if repeat_q == "Yes" or repeat_q == "yes": 
     option_right = False 
     t_class_right = False 
     repeat_question = True 
     repeat_q = " " 
    elif repeat_q == "No" or repeat_q == "no": 
     print("Okay...") 
     repeat = False 
     T_Exit() 
    else: 
     print("Please only write 'yes' or 'no'") 
    return repeat_question 

Diese Funktion später wie folgt aufgerufen wird:

elif test == 2: 
    OP_2(list_counter, repeat_question)  
    while repeat_question != True: 
     Repeat(repeat_q, option_right, t_class_right) 

alle es tut, So wiederholen Sie die Variable 'repeat_q'. Ich habe versucht, nachdem er den Wert der Variablen ändert aber nichts scheint work.i kann es an die Arbeit, wenn ich es aus der Funktion übernehmen, wie folgt aus:

elif test == 3: 
    OP_3(list_counter, repeat_question) 
    while repeat_question != True: 
     repeat_q = input("\n\nWould you like to see any other class details? ") 
     if repeat_q == "Yes" or repeat_q == "yes": 
      option_right = False 
      t_class_right = False 
      repeat_question = True 
     elif repeat_q == "No" or repeat_q == "no": 
      print("Okay...") 
      repeat = False 
      T_Exit() 
     else: 
      print("Please only write 'yes' or 'no'") 

Dies ist, was mit dem Code passiert:

Hello and welcome to my quiz. 
Please write your name: teacher 

Write the number of the class you would like to see (1, 2 or 3): 1 
Opening Class 1 

_________________________________________ 
|           | 
| 1) Alphabetical with highest score | 
| 2) Highest score, highest to lowest | 
| 3) Average score, highest to lowest | 
|_________________________________________| 

Choose one of the options above (1, 2 or 3): 2 
Sorting by highest score, from highest to lowest 

['Lol: 10', 'Zaid: 9', 'Abdul: 8', 'Hello: 5', 'Bruno: 5'] 


Would you like to see any other class details? yes 


Would you like to see any other class details? yes 


Would you like to see any other class details? yes 


Would you like to see any other class details? yes 


Would you like to see any other class details? no 
Okay... 

You have now finished viewing class details 
Thank you for using this program 
>>> # Exits the program 

Im Gegensatz dazu (die Arbeit, aber ineffiziente Version):

Hello and welcome to my quiz. 
Please write your name: teacher 

Write the number of the class you would like to see (1, 2 or 3): 1 
Opening Class 1 

_________________________________________ 
|           | 
| 1) Alphabetical with highest score | 
| 2) Highest score, highest to lowest | 
| 3) Average score, highest to lowest | 
|_________________________________________| 

Choose one of the options above (1, 2 or 3): 1 
Sorting alphabetically... 

['Abdul: 8', 'Bruno: 5', 'Hello: 5', 'Lol: 10', 'Zaid: 9'] 


Would you like to see any other class details? yes 

Write the number of the class you would like to see (1, 2 or 3): 1 
Opening Class 1 

_________________________________________ 
|           | 
| 1) Alphabetical with highest score | 
| 2) Highest score, highest to lowest | 
| 3) Average score, highest to lowest | 
|_________________________________________| 

Choose one of the options above (1, 2 or 3): 3 
Sorting by average score, from highest to lowest 

['Lol: 10.0', 'Zaid: 8.333333333333334', 'Abdul: 5.333333333333333', 'Hello: 3.3333333333333335', 'Bruno: 3.0'] 


Would you like to see any other class details? no 
Okay... 

You have now finished viewing class details 
Thank you for using this program 
>>> 

Kann jemand bitte helfen Sie mir. Eine Vollversion von meinem Code ist verfügbar, wenn jemand will, dass überprüfen: http://www.filedropper.com/teacherprogramtask3-annotated

+0

Bitte lesen Sie diese http://stackoverflow.com/help/how-to-ask und Frage. –

+0

Danke ... Ich wusste einfach nicht, wie man die Codeformatierung macht, die Zondo gemacht hat. –

Antwort

2

Wenn Sie den Teil des Codes innerhalb der Repeat() Funktion haben, repeat_question = True eine lokale Variable repeat_question genannt wird, zu schaffen. Diese Variable ist eine andere Variable als die außerhalb der Funktion, die die While-Schleife prüft.

Dieses Problem mit variablem Umfang zu tun ist, erklärt diese Website es ganz gut: http://gettingstartedwithpython.blogspot.com.au/2012/05/variable-scope.html

Es jedoch eine einfache Lösung ist, man muss nur durch die return repeat_question am Ende Ihrer Funktion repeat_question zu der Einstellung, Ergebnis der Funktion:

while repeat_question != True: 
     repeat_question = Repeat(repeat_q, option_right, t_class_right) 

auch innerhalb der elif...: und else: Aussagen der Funktion können Sie repeat_question-False setzen müssen.

Das sollte es beheben.


EDIT: Ich denke, dass es nicht so funktioniert, wenn es innerhalb der Funktion ist, die option_right und t_class_right Variablen sind auch nur lokale Variablen und wirken sich nicht auf die option_right und t_class_right, die außerhalb der Funktion sind .

Es ist ein bisschen eine chaotische Abhilfe, aber es sollte funktionieren.


OK Ich habe es funktioniert! Repeat-Funktion:

def Repeat(repeat_q, option_right, t_class_right): 
    repeat_q = input("\n\nWould you like to see any other class details? ") 
    if repeat_q == "Yes" or repeat_q == "yes": 
     option_right = False 
     t_class_right = False 
     repeat_question = True 
     repeat_q = " " 
    elif repeat_q == "No" or repeat_q == "no": 
     print("Okay...") 
     repeat = False 
     t_class_right = True 
     repeat_question = False 
     T_Exit() 
    else: 
     print("Please only write 'yes' or 'no'") 
    return repeat_question, t_class_right, repeat_question 

Und im Code:

if test == 1:        
     OP_1(list_counter, repeat_question) 
     while repeat_question != True:  
      repeat_question, t_class_right, repeat_question = Repeat(repeat_question, option_right, t_class_right) 
    elif test == 2: 
     OP_2(list_counter, repeat_question) 
     while repeat_question != True: 
      repeat_question, t_class_right, repeat_question = Repeat(repeat_question, option_right, t_class_right) 
    elif test == 3: 
     OP_3(list_counter, repeat_question) 
     while repeat_question != True: 
      repeat_question, t_class_right, repeat_question = Repeat(repeat_question, option_right, t_class_right) 
    else: 
     T_Broke() 

Sie haben repeat_q zu Namen in dem Funktionsaufruf repeat_question, wie es in dem obigen Code. Das hat möglicherweise dazu geführt, dass es abstürzte und nicht mehr funktionierte, oder es konnte geschlossen werden, da option_right und t_class_right nicht aktualisiert wurden, oder eine Kombination aus beidem.

+0

Danke SOOOOO viel Charlton, es hat sehr geholfen. –

+0

Kein Problem :). Wenn diese Antwort Ihr Problem gelöst hat, können Sie es als akzeptiert markieren. –

Verwandte Themen