Python function code help

I am getting an indentation error, why is that?

start of code

sum = 0

num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

def evenNums(num1,num2):
        if num1%2==0 or num2%2==0:
            sum+=1
            return sum
        elif num1%2==0 and num2%2==0:
            sum+=2
            return sum
        else:

print evenNums(num1,num2)

Thanks

It is 3, I did try having code after the else but it still did not work

I also tried this. The problem is with something else

Although i don’t see any point in this code. I think the problem would be fixed if you declare the sum variable inside the def evenNums.

Your code has several serious issues. Your “else” block is empty. Either delete that block or return something from that block. You cannot update “sum” in your function. You have to make the “sum” global. Like below

sum = 0

num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

def evenNums(num1,num2):
    global sum
    if num1%2==0 or num2%2==0:
        
        sum+=1
        return sum
    elif num1%2==0 and num2%2==0:
        sum+=2
        return sum
    else:
        return sum

print(evenNums(num1,num2))

Note: Using “global” in a code is not suggested or not a good practice. You may declare the sum inside the function like below

num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

def evenNums(num1,num2):
    sum = 0
    if num1%2==0 or num2%2==0:
        
        sum+=1
        return sum
    elif num1%2==0 and num2%2==0:
        sum+=2
        return sum
    else:
        return sum

print(evenNums(num1,num2))
1 Like

Thank you so much, I understand but my reasoning was that I was only using this along and for practice, plus it is the only variable I am using.

You are welcome. Of course, for practice, you can do what ever you want.
Happy codding!