Home > Software design >  Guess the number game - want to give users hints but the code doesn't work
Guess the number game - want to give users hints but the code doesn't work

Time:02-24

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.")

CodePudding user response:

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 % 30
  • 10 % 31
  • 10 % 42
  • 12 % 40

So if remainder is 0, it is divisible by that number.

CodePudding user response:

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.")

CodePudding user response:

print('Enter number between 1 and 10:')
x = int(input())
if x >= 1 and x <= 10:
    if x % 2 == 0:
        print('Even') 
    else:
        print('odd')
else:
    print("Enter number between 1 and 10")
  • Related