I am relatively new to python and I can't understand why does this throws an error.
ar=''
def decToBin(no):
while(no>0):
ar=ar+str(no%2)
no=no//2
print(ar[::-1])
decToBin(4)
code that works
def decToBin(no):
ar=''
while(no>0):
ar=ar+str(no%2)
no=no//2
print(ar[::-1])
decToBin(4)
The scope of the "ar" variable is supposed to be global and should be accessible inside the function. Can anyone explain why the former isn't working?