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)
我正在尝试插入一个计分器,为什么它不起作用?有人能给我解决方案吗?我为我糟糕的英语感到抱歉。
发布于 2018-06-09 02:52:07
调用函数并将值赋给一个变量,然后打印。请注意,变量作用域是函数的局部变量,从外部调用它需要一些特殊的声明global
。
Score = 0
def quiz(demand,correct):
global Score
发布于 2018-06-09 02:52:06
问题出在作用域上,每次调用quiz
时score
都会设置为0。最快的解决方案如下所示
Score=0
def quiz(demand,correct):
print(" ")
<everything else is the same>
https://stackoverflow.com/questions/50766768
复制相似问题