我正在为一所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)
所有这一切都是重复' repeat_q'变量。我已经尝试在它之后更改变量的值,但似乎没有任何工作。如果我将它从函数中取出,我可以使它工作,如下所示:
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
与此相反(工作但效率低下的版本):
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
>>>
任何人都可以帮助我。如果有人想查看我的代码,我可以使用完整版的代码:http://www.filedropper.com/teacherprogramtask3-annotated
答案 0 :(得分:2)
当你在Repeat()
函数中有大量代码时,repeat_question = True
正在创建一个名为repeat_question的局部变量。此变量是与while循环检查的函数外部的变量不同的变量。
这个问题与变量范围有关,这个网站很好地解释了它:http://gettingstartedwithpython.blogspot.com.au/2012/05/variable-scope.html
有一个简单的解决方案,但您只需要在函数结尾使用return repeat_question
,方法是将repeat_question
设置为函数的结果:
while repeat_question != True:
repeat_question = Repeat(repeat_q, option_right, t_class_right)
同样在函数的elif...:
和else:
语句中,您可能需要将repeat_question
设置为False
。
那应该解决它。
编辑:我认为它不在函数内部工作,option_right
和t_class_right
变量也只是局部变量,不会影响{ {1}}和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
你必须在函数调用中将repeat_q命名为repeat_question,就像在上面的代码中一样。这可能导致它崩溃并停止工作,或者由于option_right和t_class_right没有被更新,或者两者的组合,它可能已经关闭。