python采用raw

本文较为详细的介绍了python中raw_input的用法,使用raw_input 能够很方便的丛控制台读入数据。具体用法示例如下:

1.输入字符串

#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)

2.输入整数

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

3.输入浮点型

fweight = 0.0
fweight = float(raw_input(“input your weight\n”))
print ‘your weight is %f’ % fweight

4.输入16进制数据

nhex = int(raw_input(‘input hex value(like 0x20):\n’),16)
print ‘nhex = %x,noct = %d\n’ %(nhex,nhex)

5.输入8进制数据

noct = int(raw_input(‘input oct value(like 020):\n’),8)
print ‘noct = %o,ndec = %d\n’ % (noct,noct)

本文示例对python初学者有一定的学习借鉴价值,感兴趣的读者可以动手调试运行一下本文示例,以加深对raw_input用法的认识。

Posted in 未分类

发表评论