I have accessed a global variable called counter
using the keyword global
however the function does not return the global variable. Oddly, print
works but not return
.
FYI: The function below is recursive and is supposed to take as input any integer and output the number of steps required to reduce it to 0, depending on whether it's odd or even at each iteration.
counter = 0
def number_of_steps(num):
global counter
if num == 0:
return(counter)
elif num % 2 == 0:
num = num / 2
counter += 1
number_of_steps(num)
else:
num -= 1
counter += 1
number_of_steps(num)
number_of_steps(2) #expect => 2, but actually get NoneType