简体   繁体   中英

How do I check if the user's input is correct and output that they were correct?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM