samedi 27 février 2021

Not able to execute the following python code

Code

role = 'admin'

def f():
    del role

if role == 'admin':
    f()
else:
    pass

Output

Traceback (most recent call last):
  File "C:\Users\Hari\PycharmProjects\Card_Prj\ch.py", line 6, in <module>
    f()
  File "C:\Users\Hari\PycharmProjects\Card_Prj\ch.py", line 3, in f
    del role
UnboundLocalError: local variable 'role' referenced before assignment

If a role called admin exists then it should delete the variable role otherwise it should do nothing, that is what the code is doing.

I have tried different ways of executing this code but failed to do so.

Any help is highly appreciated!!!

Aucun commentaire:

Enregistrer un commentaire