raw_input() 的行为 [英] Behaviour of raw_input()

查看:44
本文介绍了raw_input() 的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解以下代码中 raw_input 的行为.我知道 num 将是字符串.不管我输入什么数字,它总是输入 elif 部分,即如果 num 是 5,它应该转到 if num 部分或者如果 num 是 10 应该去else 部分.每次它都会elif.我认为比较 STRING 和 INT 可能会抛出异常(我不这么认为)但以防万一,所以我包含了 try except 但正如预期的那样,它没有抛出任何异常.但让我感到困惑的是,为什么即使给定的输入是 10,它也总是点击 elif,至少在那种情况下我期待输出 Equal

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

请 PYTHON 大师解开谜团 :)

Please, PYTHON gurus, solve the Mystery :)

推荐答案

raw_input 返回一个字符串.所以使用int(raw_input()).

raw_input returns a string. So use int(raw_input()).

有关字符串和整数比较的工作原理,请查看此处.

And for how string and int comparsions work, look here.

这篇关于raw_input() 的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆