如何在另一个函数中使用函数内部的值?

时间:2015-05-03 07:56:42

标签: python loops if-statement tkinter

我正在制作一个程序来构建测验。 我需要从第一个循环中获取“真实”,以便将其用于第二个循环。

就像现在一样,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..")

1 个答案:

答案 0 :(得分:1)

Pass是一个参数:

def final_count(true):
    if true==2:   
        print("All of the answers were answered correctly!")
    elif true==1:
        print("50% were answered correctly")
    else:
        print("none of the answers were right..")

因为您return函数中的verif,您只需运行

即可
true = verif()
final_count(true)

我建议将变量名称从true更改为count或基本上任何其他内容。此外,在处理局部变量时,您不必保持相同的名称