0

I am trying to write a program that takes reports and adds them to a list of tuples. The program should also make a dictionary with the value as a list of two integers. But when I try to change the global values of the two counter I get the error: "UnboundLocalError: local variable 'False_count' referenced before assignment".

report_list = []
success = True

report1 = ('nano1', 'Frida', False)
report2 = ('bio_x', 'Arnar', True)
report3 = ('nano1', 'Frida', True)

True_count = 0
False_count = 0
status_dict = {'nano1': [0,0], 'bio_x': [0,0]}



def report_list_maker(report_tuple):
    report_list.append(report_tuple)
    if report_tuple[2] == True:
        True_count += 1
    elif report_tuple[2] == False:
        False_count += 1
        print("False")


    temp_dict = {report_tuple[0]:[False_count, True_count]}
    status_dict.update(temp_dict)

report_list_maker(report1)
report_list_maker(report2)
report_list_maker(report3)

print(report_list)
print(status_dict)
tripleee
  • 175,061
  • 34
  • 275
  • 318

1 Answers1

-1

You can use

def report_list_maker(report_tuple):
    global True_count, False_count
    ...

Quoting from the official documentation:

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Jan
  • 42,290
  • 8
  • 54
  • 79