python question

+11 votes
asked Aug 6 by 조강민 (190 points) 2 flags
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

+3 votes
answered Aug 8 by Akshay Maloo (260 points)
edited Aug 12 by Akshay Maloo

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

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

add int(input())

OR

we can convrt directly the the no 5 into "5" string

commented Aug 19 by vishal8112 (140 points)
when i=5 correct
commented Aug 20 by Rahul Choubey (480 points)
Why is this question getting flagged? I see nothing wrong.
+2 votes
answered Aug 9 by Le Mysterio (180 points)
This code isn't working properly because the input() function takes string as input.. Not int or floating point values..
In the if statement you wrote: you set the condition to something like this: if input == 5. Here, 5 which is an int doesn't match the actual input "5" a string.. That's why. Here is the fixed script if you need.

userInput = input("Enter your number: ")

if userInput == "5": # Here 5 is a string
    print("I ate all my toes.")
else:
    print("I didn't eat all my toes.")

Hope I have helped...
0 votes
answered Aug 10 by LiOS (5,280 points)
When reading user input in Python, it is always taken as string even if a user enters a number.

As a result in this case, you could either carry on reading in string and convert the input to integer or read the input directly as an integer by:

i = int(input("What's the value of 2+3?\n"))
0 votes
answered Aug 11 by Prashant Kamble (140 points)
Because they have not mentioned answering variable I printed after double inverted commas and should be separated by comma
0 votes
answered Aug 15 by Sathvi Karpurapu (140 points)
it is so beacause u are taking input as string

so change the first line of your code

i=int(input("What is the value of 2+3\n"))
0 votes
answered Aug 16 by Rohan (240 points)

THIS CODE SHOULD WORK PROPERLY:-

s1 = "What's the value of 2+3?

a1 = "5"

i1 = int(input(s1))

if i1 == a1 :

    print("Gotcha!")

else :

   print("Uh uh! Wrong one!")

0 votes
answered Aug 17 by Abitha (150 points)

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

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

0 votes
answered Aug 19 by vishal8112 (140 points)
i is getting string as the input.int &string cannot manipulate in sometime in the program.so that it is Failed
0 votes
answered Aug 20 by Rahul Choubey (480 points)
edited Aug 26 by Rahul Choubey

input() takes its input as a string, so there are two options:

1. use int(input()) instead

The downside is that you could get an error if the user enters an explicit string such as "pie"

To get around this, here's the full solution:

try:

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

except:

    print("Failed...")

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

2. change 5 to a string

downside: it looks strange

here's the full code(it sort of gets around this issue)

i = input("What's the value of 2+3?\n")
if i == str(5):
    print("Correct!")
else:
    print("Failed...")

0 votes
answered Aug 22 by BHARAT Aggarwal (140 points)
i=input("What's the value of2+3?\n")

if i==5:
     print("Correct!")
else:
     print("Failed...")
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.
...