1

Im trying to get a points system going but whenever I run it says:

UnboundLocalError : Local variable 'teambc' refrenced before assignment:

Here is my code:-

teambc = 0
teamac = 0
def bst_q(question, answer):

    rq = input(str(question))
    if rq == answer:
        print ('Correct, Well done')
        teambc += 1   
    elif rq != answer:
        print ('wrong. Team A now')
        rq = input(str(question))
        if rq == answer:
            print ('Correct, Well done')
            teamac += 1   
    elif rq != answer:
        print ('wrong. Both Wrong!')

bst_q('What is 13 times 2?','26')
Bandook
  • 658
  • 6
  • 21
A.Salihu
  • 21
  • 2
  • You need to either pass `teamac` and `teambc` to the function or write `global teambc ` and `global teamac` at the start of the function, given they are global variables. – Ardweaden Apr 09 '20 at 14:28
  • https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Ardweaden Apr 10 '20 at 07:02

1 Answers1

0

It depends on what you need to do with that two variables. Here are two working code snippets, choose the one that you need:

def bst_q(question, answer):
  teambc = 0 
  teamac = 0
  rq = input(str(question))
  if rq == answer:
      print ('Correct, Well done')
      teambc += 1
  elif rq != answer:
      print ('wrong. Team A now')
      rq = input(str(question))
      if rq == answer:
          print ('Correct, Well done')
          teamac += 1
  elif rq != answer:
      print ('wrong. Both Wrong!')

bst_q("What's your name?","Luigi")