Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

python question

+11 votes
asked Aug 6, 2020 by 조강민 (190 points)
i = input("What's the value of 2+3?\n")

if i == 5:
    print("Correct!")
    
else:
    print("Failed...")

why this code doesn't work correctly?

13 Answers

0 votes
answered Aug 28, 2020 by Mudasir Momand (230 points)
It should look like: i  = int(input("User input: "))

since you are asking for an integer, then 'user input' should be set as int. Your if statement is looking for a int value, not a string value, for just a quick fix you could do: if i == "5" as you can see I have strung the num value to a string value. This should now equal string to string.
0 votes
answered Aug 31, 2020 by saptarshibasu15 (140 points)

i = input("What's the value of 2+3?\n")

if int (i) == 5:
    print("Correct!")
    
else:
    print("Failed...")
 

*the input function returns a string..and we cannot compare it an integer directly without converting it into an int.​

0 votes
answered Sep 1, 2020 by Roshan (180 points)
Because, a string '5' is stored in i not an int. So, its comparing int with str, which results in failure of this program.

Solution:

if int(i) == 5:
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...