0

This is the code in question:

#i/p from user print "enter your number" a=sys.stdin.readline() if(a==6): print('entered a 6!') else: print('you did not enter a 6') 

If I enter 6, it is supposed to return entered a 6!,but it is returning you did not enter a 6.

Why is this happening?

1 Answer 1

3

You should use:

a = int(sys.stdin.readline())

or

a = int(raw_input())

Your code is reading the input as a string, and you are comparing it to an integer. You need to convert the input for a to an int before making the comparison.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer. This is called "type conversion", "type casting" or "type coercion"
Thank you so much!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.