0

Can anyone see why I get "UnBoundLocal Error" for my score varible?

import random
Score=0
def Main_Menu(Score):
    print("=============================")
    print("WELCOME TO MY QUIZ")
    print("=============================")
    while True:
        Username=input("What is your name?")
        if Username.isalpha():
            if len(Username)>11:
                print("You are only a maximum of 11 characters")
            else:
                Username=Username.title()
                break
        else:
            print("Letters only")
    while True:
        while True:
            Option=input("What do you want to do?\n1 For Quiz\n2 To Quit.")
            if Option.isdigit():
                Option=int(Option)
                break
            else:
                print("Numbers Only.")
        if Option==1:
            for x in range(10):
                Quiz(Username)
            print("You have scored",Score,"point out of 10!!\n")
        elif Option==2:
            input("Press Enter to quit the program")
            break
        else:
            print("You only have 2 options")

    Quiz(Username)
def Quiz(Username):
    Tries=3
    Number_One=random.randint (0,10)
    Number_Two=random.randint (0,10)
    Arithmetic_Operator=random.choice(["+","-","*",])
    if Arithmetic_Operator=="+":
        print(Username,"\nWhat is",Number_Two,"+",Number_One,"?")
        Answer=Number_Two+Number_One      
    elif Arithmetic_Operator=="-":
        print(Username,"\nWhat is",Number_Two,"-",Number_One,"?")
        Answer=Number_Two-Number_One
    elif Arithmetic_Operator=="*":
        print(Username,"\nWhat is",Number_Two,"*",Number_One,"?")
        Answer=Number_Two*Number_One 
    while Tries!=0:
        while True:
           Guess=input("Answer: ")
           if Guess.isdigit():
                Guess=int(Guess)    
                break
           else:

                print("Numbers Only.")
        if Guess==Answer:
            print("Well Done.You got it right.\nYou have a point")
            Score=Score+1

            break
        elif Guess!=Answer:
            Tries=Tries-1
            print("You  have",Tries,"tries left")
        if Tries==0:
            print("The answer is",Answer)

Main_Menu(Score)
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
John
  • 1

1 Answers1

0

You are getting the UnboundLocalError: local variable 'Score' referenced before assignment error because the Quiz method doesn't have a local variable Score but is trying to read from it. You probably meant to use the the global variable Score, but it's not available in the method scope without using the global statement. You can fix this by adding global Score in the beginning of the Quiz method.

However, using globals in python code is another discussion, I would strongly recommend avoiding them, unless there's really a proper reason to use them. Please see this SO for more details and discussion.

Community
  • 1
  • 1
lekksi
  • 4,868
  • 1
  • 16
  • 13