函数中的循环不工作时(Python 3.5.0)

函数中的循环不工作时(Python 3.5.0),python,function,while-loop,Python,Function,While Loop,我正在为一所有7-10岁学生的学校编写一段代码,测试他们的基本算术技能。该程序功能齐全,应允许教师在事后查看文本文件中的分数。然而,它目前有556行。我已经尽可能地缩短了它,但我似乎无法让这一段代码正常工作: 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 ==

我正在为一所有7-10岁学生的学校编写一段代码,测试他们的基本算术技能。该程序功能齐全,应允许教师在事后查看文本文件中的分数。然而,它目前有556行。我已经尽可能地缩短了它,但我似乎无法让这一段代码正常工作:

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
此函数稍后将按如下方式调用:

elif test == 2:
    OP_2(list_counter, repeat_question)    
    while repeat_question != True:
        Repeat(repeat_q, option_right, t_class_right)
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'")
它所做的就是重复'repeat_q'变量。我已经尝试过在它之后更改变量的值,但似乎没有任何效果。如果我将它从函数中取出,我可以让它工作,如下所示:

elif test == 2:
    OP_2(list_counter, repeat_question)    
    while repeat_question != True:
        Repeat(repeat_q, option_right, t_class_right)
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'")
这就是代码发生的情况:

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
    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()
与此相反(工作但效率低下的版本):


谁能帮帮我吗。如果有人想查看,我的代码的完整版本是可用的:

当您在
Repeat()
函数中有代码块时,
Repeat\u question=True
正在创建一个名为Repeat\u question的局部变量。此变量与while循环正在检查的函数外部的变量不同

这个问题与可变范围有关,本网站对此做了很好的解释:

但是,有一个简单的解决方案,您只需在函数末尾使用
return repeat\u question
,将
repeat\u question
设置为函数的结果:

    while repeat_question != True:
        repeat_question = Repeat(repeat_q, option_right, t_class_right)
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
在函数的
elif…:
else:
语句中,您可能必须将
repeat\u question
设置为
False

这应该可以解决问题


编辑:我认为它不像在函数内部那样工作,
option\u right
t\u class\u right
变量也只是局部变量,不会影响函数外部的
option\u right
t\u class\u right

这是一个有点混乱的解决办法,但它应该工作


好的,我成功了! 重复功能:

    while repeat_question != True:
        repeat_question = Repeat(repeat_q, option_right, t_class_right)
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
在守则中:

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

您必须将repeat_q命名为repeat_q,才能在函数调用中重复这个问题,正如上面的代码中一样。这可能导致它崩溃并停止工作,也可能因为option_right和t_class_right未被更新而关闭,或者两者的组合。

Repeat()
函数中有代码块时,
Repeat\u question=True
正在创建一个名为Repeat\u question的局部变量。此变量与while循环正在检查的函数外部的变量不同

这个问题与可变范围有关,本网站对此做了很好的解释:

但是,有一个简单的解决方案,您只需在函数末尾使用
return repeat\u question
,将
repeat\u question
设置为函数的结果:

    while repeat_question != True:
        repeat_question = Repeat(repeat_q, option_right, t_class_right)
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
在函数的
elif…:
else:
语句中,您可能必须将
repeat\u question
设置为
False

这应该可以解决问题


编辑:我认为它不像在函数内部那样工作,
option\u right
t\u class\u right
变量也只是局部变量,不会影响函数外部的
option\u right
t\u class\u right

这是一个有点混乱的解决办法,但它应该工作


好的,我成功了! 重复功能:

    while repeat_question != True:
        repeat_question = Repeat(repeat_q, option_right, t_class_right)
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
在守则中:

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

您必须将repeat_q命名为repeat_q,才能在函数调用中重复这个问题,正如上面的代码中一样。这可能导致它崩溃并停止工作,也可能因为选项右和t类右未更新而关闭,或者两者兼而有之。

请阅读此内容并提出问题。谢谢。。。我只是不知道如何做Zondo所做的代码格式化工作。请阅读此内容并提问。谢谢。。。我只是不知道如何做Zondo所做的代码格式化工作。非常感谢Charlton,它帮助了很多。非常感谢Charlton,它帮助了很多。