-2

I'm creating a python quiz game for my class. However, I'm having difficulty with wrong answers throwing me a "UnboundLocalError: local variable 'score' referenced before assignment" error. There is also the fact that score will not accumulate, or add up to the total at the end of the game. I'm concerned once I figure out to properly let my program replay these issues will only complicate things further. I've looked at a few other threads and tried methods like changing "score=+1" to a different variant like score= score+1 but I can't figure this out with my level of experience.

ques1=['A. Blue', 'B. Red', 'C. Green', 'D. Yellow']#answers for question 1
ques2=['A. Solvent', 'B. Saltwater', 'C. Saliva', 'D. Syrup']#answers for question 2
ques3=['A. 2', 'B. 12', 'C. 21', 'D. 100']#answers for question 3
ques4=['A. Bryan Cranston', 'B. Chris Evans', 'C. Snoop Dogg', 'D. Arnold Schwarzenegger']#answers for question 4
ques5=['A. False', 'B. Apple', 'C. The Sky', 'D. Saltwater']#answers for question 5

score = 0
wrong = 0

def question1():#Ask the first question and calculate score depending on answer
    print("Question one! What color is the sky?")
    for ques in ques1:
        print(ques)
    ans1=input("")
    if ans1 == "A":
        score=+1
        print("Correct answer! You get one point! You currently have a score of ",score," !")
    elif ans1 == "B":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans1 == "C":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans1 == "D":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    else:#Force them to answer until something valid is entered
        print("Input something valid! A, B, C, or D!")


def question2():#Ask the second question and calculate score depending on answer
    print("Question Two! What liquid is the ocean made of?")
    for ques in ques2:
        print(ques)
    ans2=input("")
    if ans2 == "B":
        score=+1
        print("Correct answer! You get one point! You currently have a score of ",score," !")
    elif ans2 == "A":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans2 == "C":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans2 == "D":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    else:#Force them to answer until something valid is entered
        print("Input something valid! A, B, C, or D!")


def question3():#Ask the third question and calculate score depending on answer
    print("Question Three! What is the legal drinking age in Ohio?")
    for ques in ques3:
        print(ques)
    ans3=input("")
    if ans3 == "C":
        score=+1
        print("Correct answer! You get one point! You currently have a score of ",score," !")
    elif ans3 == "A":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans3 == "B":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans3 == "D":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    else:#Force them to answer until something valid is entered
        print("Input something valid! A, B, C, or D!")


def question4():#Ask the fourth question and calculate score depending on answer
    print("Question Four! Who played the teminator in the movie Terminator?")
    for ques in ques4:
        print(ques)
    ans4=input("")
    if ans4 == "D":
        score=+1
        print("Correct answer! You get one point! You currently have a score of ",score," !")
    elif ans4 == "A":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans4 == "C":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans4 == "B":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    else:#Force them to answer until something valid is entered
        print("Input something valid! A, B, C, or D!")


def question5():#Ask the fifth question and calculate score depending on answer
    print("Question Five! What is the opposite of true?")
    for ques in ques5:
        print(ques)
    ans5=input("")
    if ans5 == "A":
        score=+1
        print("Correct answer! You get one point! You currently have a score of ",score," !")
    elif ans5 == "B":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans5 == "C":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans5 == "D":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    else:#Force them to answer until something valid is entered
        print("Input something valid! A, B, C, or D!")


def scoretotal():#Display the score
    print("In the end you got a total of ",score," correct and ",wrong, " wrong.")


def playagain():#Repeats all previous questions if the user wants, continuing to build score up
    print("Want to play again without fully restarting? I don't know how to let you!")


def main():#Assemble program steps
    print("Time to play a trivia game! You will be asked a list of questions in order! Answer with an uppercase A, B, C, or D.")
    question1()
    question2()
    question3()
    question4()
    question5()
    scoretotal()
    #playagain()


#activate program
main()
xilpex
  • 3,097
  • 2
  • 14
  • 45
  • Does this answer your question? [Python 3: UnboundLocalError: local variable referenced before assignment](https://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment) – AMC May 03 '20 at 01:30
  • As an aside, this program is begging for some abstraction. – AMC May 03 '20 at 01:31

3 Answers3

1

The reason is because score is not seen as a global variable, and so isn't being picked up in within each question.

I would use the global function inside each question, like this:

score = 0
wrong = 0

def question1():#Ask the first question and calculate score depending on answer
    print("Question one! What color is the sky?")
    global score
    for ques in ques1:
        print(ques)
    ans1=input("")
    if ans1 == "A":
        score=+1
        print("Correct answer! You get one point! You currently have a score of ",score," !")
    elif ans1 == "B":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans1 == "C":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    elif ans1 == "D":
        wrong=+1
        print("Uh oh, wrong answer! You currently have a score of ",score," !")
    else:#Force them to answer until something valid is entered
        print("Input something valid! A, B, C, or D!")

If you add this to every question, it will work.

Within each function, Python doesn't know if the variables should be used locally (just in that function) or everywhere (globally), and so it defaults to just locally.

Even though you have defined it outside the function, as it hasn't been defined inside, it doesn't recognize score as a variable (hence why it says that score is being referenced, before being defined).

And finally, when doing augmented assignments, the operator comes BEFORE the equals sign. So score should be defined like:

score += 1
c_n_blue
  • 182
  • 2
  • 10
  • https://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment – AMC May 03 '20 at 01:30
0

A few things wrong with your code. The first is that score is not considered a global variable. You need to add this line:

    global score

To the beginning of your question functions. Also, incrementing score should be done like this:

score += 1

So that score is not equal to positive one, but rather incremented by one

User 12692182
  • 927
  • 5
  • 16
0

"UnboundLocalError: local variable 'score' referenced before assignment" This happen when you try to access a variable that is defined outside before the function. Ex.

n = 0
def func():
    return n + 1

To avoid this you can do:

def func():
    global n
    return n + 1

n = 0
func(n)
oscar0urselli
  • 45
  • 1
  • 7
  • https://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment – AMC May 03 '20 at 01:30