-2

When I run the code and answer with 3, the console shows The answer is 3. This code is just an example, I worked on a code with random number.

I gave the input in the if statement, in a variable and i deleted else statement

answer = input("Answer of 1 + 2 = ")

if answer == 3:
    print("You're right!")
else:
    print("The answer was 3")

The right output would be You're right!

Anoop R Desai
  • 712
  • 5
  • 18
Criepstar
  • 59
  • 8
  • the **+** "signal plus" adding the input value and not combining it, let's say you entered 1 and 2, what happened it parsed as string so the output is 12 not 3 so you need to use int (answer) – Ahmad Sep 10 '19 at 18:21

2 Answers2

1

Typecasting to the rescue!

answer = input("Answer of 1 + 2 = ")

if int(answer) == 3:
#  ^^^
    print("You're right!")
else:
    print("The answer was 3")
Jan
  • 42,290
  • 8
  • 54
  • 79
0

By default the value you get from input() is of type string. If you write 3 in your console, you get

answer = "3"

and

"3" != 3

You need to cast the input to int. Add this line before the if statement:

answer = int(answer)

Be careful to check that the typed value is actually an int (you can do that by using a try catch statement, or better with a while)

Nikaido
  • 4,443
  • 5
  • 30
  • 47