-1

The code:

user_answer = input("What is 8+13=")
answer = 21
if user_answer == answer:
    print("Correct answer!")
else:
    print("Try Again!")

Even if it takes input as 21, it prints the else part of the code

xrisk
  • 3,790
  • 22
  • 45

1 Answers1

2

The input function returns the entered text as a string, see the API documentation:

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

Then your code is comparing the string "21" to an integer value 21, comparing a string with an int will evaluate to false.

You could convert the string to an integer value (you can use int):

user_answer = input("What is 8+13")
answer=21
if int(user_answer) == answer:
    print("Correct answer!")
else:
    print("Try again!")

but this will raise an error if the input can't be converted to an int value. Alternatively convert the int to a string:

user_answer = input("What is 8+13")
answer=21
if user_answer == str(answer):
    print("Correct answer!")
else:
    print("Try again!")
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276