User51327
User51327

Reputation: 13

how to insert a score counter in python

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)

I'm trying to insert a score counter, why does not it work? can someone give me the solution? I'm sorry for my bad english.

Upvotes: 0

Views: 2693

Answers (2)

MaNKuR
MaNKuR

Reputation: 2704

Call the function and assign the value to a variable and print. Note that the variable scope is local to a function and calling it from outside requires some special declaration global.

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

Upvotes: 0

Mohammad Athar
Mohammad Athar

Reputation: 1980

The issue is scope, score gets set to zero every time you call quiz The quickest solution is as follows

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

Upvotes: 1

Related Questions