首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python中插入计分计数器

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

Stack Overflow用户
提问于 2018-06-09 02:47:49
回答 2查看 2K关注 0票数 0
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)

我正在尝试插入一个计分器,为什么它不起作用?有人能给我解决方案吗?我为我糟糕的英语感到抱歉。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-09 02:52:07

调用函数并将值赋给一个变量,然后打印。请注意,变量作用域是函数的局部变量,从外部调用它需要一些特殊的声明global

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

Stack Overflow用户

发布于 2018-06-09 02:52:06

问题出在作用域上,每次调用quizscore都会设置为0。最快的解决方案如下所示

Score=0
def quiz(demand,correct):
    print(" ")
<everything else is the same>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50766768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档