如何在python中插入分数计数器

时间:2018-06-08 18:47:49

标签: python-3.x

def quiz(demand,correct):
    print(" ")
    Score=0
    Answer=input(demand)
    Answer=Answer.lower()
    if Answer!="y" and Answer!="n":
        print("I did not understand the answer")
        quiz(demand,correct)
    elif Answer==correct:
        print("correct answer")
        Score=Score+1
        return Score
    else:
        print("wrong answer")
demand1="the Napoleon's horse is white? y/n: "
correct1="y"
quiz(demand1,correct1)
demand2="berlusconi is president of italy? y/n: "
correct2="n"
quiz(demand2,correct2)
print("score:",Score)

我试图插入分数计数器,  为什么不起作用?  有人可以给我解决方案吗?  抱歉我的英语不好。

2 个答案:

答案 0 :(得分:1)

问题是范围,每次拨打scorequiz都会设为零 最快的解决方案如下

Score=0
def quiz(demand,correct):
    print(" ")
<everything else is the same>

答案 1 :(得分:0)

调用该函数并将值赋给变量并打印。请注意,变量范围是函数的本地范围,从外部调用它需要一些特殊的声明global

Score = 0
def quiz(demand,correct):
    global Score