How can I reference variables defined within if/elif statements further on in the parent python (3.6) function?
for example, the below is a mock-up of my code - I am trying to 'print' or 'work with' variables defined in each 'if' and 'elif' block below, at the end of the function which contains the if/elif statements that the variables are defined within, however I get an unresolved reference error in pycharm and the following python error when I run the code:
UnboundLocalError: local variable 'a1_summary' referenced before assignment
I am relatively new to python and coding and don't understand where I am going wrong as I expect that as the variables are defined within the same function then they should be 'visible' to the print statement at the end of the function...
Is this a case where global variables should be used?
def Process_Data(incoming_data):
if incoming_data.count(',') == 2:
data_summary = incoming_data.split(',')
a1_summary, b1_summary = data_summary[0], data_summary[1]
elif incoming_body.count(',') == 3:
data_summary = incoming_data.split(',')
a2_summary, b2_summary, c2_summary = data_summary[0], data_summary[1], data_summary[2]
print(a1_summary, b1_summary, a2_summary, b2_summary, c2_summary )
else:
pass
Any help or advice is much appreciated - thank you.
Aucun commentaire:
Enregistrer un commentaire