-3

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!!!

Countour-Integral
  • 1,138
  • 2
  • 7
  • 21

1 Answers1

0

I would suggest you refer to some Docs to understand how Global and Local Variable work but if you want to make it work, here is the required code:

role = 'admin'

def f():
    global role
    del role

if role == 'admin':
    f()
else:
    pass
BoredRyuzaki
  • 163
  • 2
  • 10