【发布时间】:2015-05-03 07:56:42
【问题描述】:
我正在开发一个构建测验的程序。 我需要从第一个循环中取出我的“真实”,以便在第二个循环中使用它。
就像现在一样,Python 说"true" is not defined。
def verif(): #this is the function to check if answer was right
y=(chosen_answer.get())
true=0
if chosen_answer==1:
true=true+1
else:
true=true #at the end true represents the number of correct answers
return true
def final_count():
if true==2: #here it shows that "true" was not identified
print("All of the answers were answered correctly!")
elif true==1:
print("50% were answered correctly")
else:
print("none of the answers were right..")
【问题讨论】:
-
你没有任何循环,你的意思是指
verif和final_count函数? -
一般来说,使用像
true这样的语言结构作为变量名是个坏主意。final_count是一个完全独立于verif的函数,因此不能像那样共享变量。 -
确实,对不起,我指的是功能。有什么办法可以分享吗? @Huey
-
可以使用全局变量在函数之间共享
-
@Huey
true不是任何版本的 Python 中的语言结构,仅供参考。但是你的观点仍然有效。
标签: python loops if-statement tkinter