1

I'm creating a program where I output math problems and user inputs their answer. When I test out and input the right answer, it says it's the wrong answer the correct answer is the answer is inputted.

I've tried storing the answer in a variable but that didn't change anything. I also tried putting them in parenthesis which didn't work as well.

    numberone = (randint(1, 10))
    numbertwo = (randint(1, 10))

    print(f"Sara biked at {numberone}mph for {numbertwo} hour(s). How far 
    did she travel?")
    answer= input("Enter answer here:")
    correct = (numberone*numbertwo)
    if answer == correct:
      print("Correct!")
    else:
      print(f"Sorry that is the wrong answer. The correct answer is 
      {correct}.")

I input numberone*numbertwo but it said I was wrong.

Hema
  • 13
  • 3
  • Strings are not numbers. `input()` returns a string. You need to use `int()` to convert that string to an integer. – John Coleman Sep 13 '19 at 01:46
  • See [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) for some tips. – martineau Sep 13 '19 at 01:49

1 Answers1

2

You are comparing an integer against a value from input(), which returns a string. Change the input line to be wrapped in the int() function.

numberone = (randint(1, 10))
numbertwo = (randint(1, 10))

print(f"Sara biked at {numberone}mph for {numbertwo} hour(s). How far 
did she travel?")
answer= int(input("Enter answer here:")) # wrap the input in int() to compare a number not a string
correct = (numberone*numbertwo)
if answer == correct:
  print("Correct!")
else:
  print(f"Sorry that is the wrong answer. The correct answer is 
  {correct}.")
MurrayW
  • 393
  • 2
  • 10