Reputation: 11
x
is a random number between 1 and 10 and users have to guess it. Basically, I want to give them hints, but the code doesn't work.
print("Hint:")
if guess == x/2 == int :
print("The target number is even.")
if guess == x/2 == float :
print("The target number is uneven.")
Upvotes: 0
Views: 76
Reputation: 31
Instead use modulus operator to check if it's even or odd:
if x % 2 == 0:
print("Even")
else:
print("Odd")
Using it returns the remainder of the division.
9 % 3
→ 0
10 % 3
→ 1
10 % 4
→ 2
12 % 4
→ 0
So if remainder is 0
, it is divisible by that number.
Upvotes: 2
Reputation: 19242
To check the oddness/evenness of a number, you should use the modulus operator. You shouldn't attempt to directly typecheck the result of the division:
guess = 1
print("Hint:")
if guess % 2 == 0 :
print("The target number is even.")
else:
print("The target number is odd.")
Upvotes: 0