下面介绍了python raw_input的用法,
使用raw_input 能够很方便的丛控制台读入数据。
#13222319810101**** nID = '' while 1: nID = raw_input("Input your id plz") if len(nID) != len("13222319810101****"): print 'wring length of id,input again' else: break print 'your id is %s' % (nID)
nAge = int(raw_input("input your age plz:\n")) if nAge > 0 and nAge < 120: print 'thanks!' else: print 'bad age' print 'your age is %d\n' % nAge
fWeight = 0.0 fWeight = float(raw_input("input your weight\n")) print 'your weight is %f' % fWeight
nHex = int(raw_input('input hex value(like 0x20):\n'),16) print 'nHex = %x,nOct = %d\n' %(nHex,nHex)
nOct = int(raw_input('input oct value(like 020):\n'),8) print 'nOct = %o,nDec = %d\n' % (nOct,nOct)
使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的
当输入为纯数字时
input会计算在字符串中的数字表达式,而raw_input不会。
如输入 “57 + 3”:
看python input的文档,可以看到input其实是通过raw_input来实现的,原理很简单,就下面一行代码:
def input(prompt): return (eval(raw_input(prompt)))