0

So I'm making an RFID scanner and I want to be able to set different modes, for example scanning twice to do something than scanning once, however whenever this code runs:

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
import signal
import datetime
import os
import time
import math
import MFRC522

MIFAREReader = MFRC522.MFRC522()
Detected = False

def Scan():
    counter = 0
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
    if(status == MIFAREReader.MI_OK):
        Detected = True
        counter += 1
        GPIO.cleanup()
        time.sleep(2)
    if(Detected == True and status == MIFAREReader.MI_OK):
        counter += 1
        GPIO.cleanup()

    print counter

while True:
    Scan()
    time.sleep(1)

I get this error:

UnboundLocalError: local variable 'Detected' referenced before assignment

I'm relatively new to python so I don't know what this means any research I've done doesn't really turn up any results that I understand so could someone please explain whats happening here and how to rectify these errors.

invisabuble
  • 149
  • 1
  • 3
  • 12
  • Possible duplicate of [Another UnboundLocalError in Python2.7](https://stackoverflow.com/questions/36772622/another-unboundlocalerror-in-python2-7) – pppery Aug 13 '17 at 21:31

1 Answers1

0

sicne you assign to Detected inside the function that forces it to be a local variable, and you must now explicitly declare global if it is supposed to use the global variable

def Scan():
    global Detected
    ...
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179