Can I check if anyone able to give advice for my coding below?
I am trying to enter 2 integers and the program will read if e.g 2 and 2 is even, 3 and 3 is odd, lastly if 2 and 3 it will print one is even the other is odd.
def q6():
num1 = int(input("Enter One Number: "))
num2 = int(input("Enter Second Number: "))
if (num1 % 2) == 0:
if (num2 % 2) == 0:
print("The Numbers are Even")
else:
print("One Number is Even and the other is Odd")
else:
print("The Numbers are Odd")
q6()
Output 1:
Enter One Number: 2
Enter Second Number: 5
One Number is Even and the other is Odd
Output 2:
Enter One Number: 2
Enter Second Number: 2
The Numbers are Even
Output 3:
Enter One Number: 3
Enter Second Number: 3
The Numbers are Odd
Output 4:
Enter One Number: 5
Enter Second Number: 16
The Numbers are Odd
It seems to be working, but under Output 4 it suppose to be "One Number is Even the other is Odd" but it doesn't work very well seems I am missing something..
CodePudding user response:
Your current code is not checking all four combinations. I would use this logic:
if (num1 num2) % 2 == 1:
print("One Number is Even and the other is Odd")
elif num1 % 2 == 0 and num2 % 2 == 0:
print("The Numbers are Even")
else:
print("The Numbers are Odd")
Note that the first condition if the above if
block rests on that an odd number plus an even number will always result in an odd number, whose mod 2 remainder will be 1.