0
    a = input('10+1: ')
if a == 11:
    print("correct")
else:
    print('wrong')

The code above is not working in my program.

Its giving me an output of something like this:

10+1: 11
wrong

Process finished with exit code 0
0

3 Answers 3

1

Comparision like a == 11 is between a (input string) and 11 (number).

To notate number as string add quotes around it like that: '11'.

Change to a == '11': a (input string) and '11' (string).

And it shall work like magic:

a = input('10+1: ')
# Compare to string '11'
if a == '11':
    print("correct")
else:
    print('wrong')

Example Run:

10+1: 11
correct
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot. i just started python i was getting de motivated. I can continue my self learning thanks a lot
Welcome @Samrath. Keep the good spirit, learn & succeed! You can mark as solved now.
1

An alternative solution to those in the previous answers would be to convert the input to an integer (whole number 1, 2, 3, etc.) before you compare it this can be done with the int() keyword:

a = int(input("10+1="))

if a == 11:
    print("Correct")
else:
    print("Incorrect")

This method will make your code more readable and will also raise a ValueError should the user attempt to enter something which is not a number. This is will be usful later on when you wish to make sure only specific inputs are allowed. Input validation in your example would be:

while True:
    try:
        a = int(input("10+1="))
        break
    except ValueError:
        print("Sorry that isn't a valid input!")
        pass

if a == 11:
    print("Correct")
else:
    print("Incorrect")

I may have gone a bit beyond scope with my answer but I hope this helps

Comments

0

You are comparing a string '11' with an integer 11 and hence getting the output as wrong. Try comparing a == '11' and it should give your desired output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.