pythonvariablesrandomraw-inputjes

`JES - Python, Even and Odd


I need to write a program that asks if a random number is even or odd, the user inputs the answer and then the program tells you if it is correct or incorrect. I do not know what to put in ans=raw_input("is" [random number] "Odd or Even?")

this is what i have implemented right now, what do i do to fix it?

def evenOdd():
  num=random.randrange(1,101)
  ans=raw_input("is"+num+"even or odd?")
  if ans % 2 == 0:
    print "correct"
  elif ans % 2 == 1:
    print "incorrect"

Solution

  • This should solve your problem-

    def evenOdd():
        num = random.randrange(1,101)
        ans = raw_input("Is "+str(num)+" even or odd?")
        if (num % 2 == 0 and ans == "even") or (num % 2 != 0 and ans == "odd"):
            print "Correct answer"
        else:
            print "Incorrect answer"