I wanted to understand the behaviour of raw_input in the below code.
I know num
will be string.
Irrespective of whatever number i enter it always enter the elif
part i.e. if num is 5, which should go to if num<check:
part or if num is 10 which should go to else
part. Every single time it is going to elif
. I thought comparing STRING and INT might throw exception( I dont think so) but just in case, so I had included try except
but as expected it did not throw any exception. But what puzzles me is why it is ALWAYS hitting elif
even when the input given was 10, atleast in that case i was expecting output Equal
num = raw_input('enter a number')
check = 10
try:
if num<check:
print 'number entered %s is less'%num
elif num>check:
print 'number entered %s is greater'%num
else:
print 'Equal!!!'
print 'END'
except Exception,e:
print Exception,e
Please, PYTHON gurus, solve the Mystery :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…