[Solved] Why I am getting ” Undefined Variable “?


Define r outside the Checkdiv function and in the function use r variable as a global variable with global keyword; global r and then r=int(n % d). so that other code blocks will be able to see the r variable too.

r = 0
def Checkdiv(n, d):
    global r
    r = int(n % d)
    if r==0:
        return True
    else:
        return False
n= int(input('Please enter the number to evaluate:'))
d= int(input('Please enter the divisor: '))


if Checkdiv(n,d) is True :
    print(f'{n} can be divided by {d} since the end remainder is {r}')
else: 
    print (f'{n} cannot be divided by {d} since remainder is {r}')

1

solved Why I am getting ” Undefined Variable “?