python 笔记 raw_input()与input()的区别——12.24
习题 11:提问
开门见山,先敲点代码
ex11.py
# -*- coding: utf-8-*-
print "How old are you?",
age =raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()
print "So you're %r old, %r tall and %rheavy."% (
age, height, weight)
运行结果:
感悟与自我测试:
在此简单说下%r和%s的个人理解
%r是string转义成repr,输出对python比较友好
%s直接是str,它的输出对于用户比较友好
raw_input(),python的内建函数,通过读取读取控制台的输入实现用户交互。
相似的内建函数还有input(),
raw_input直接读取控制台的输出,支持任意类型的输入。
Input()则希望能够读取一个合法的python表达式,必须使用引号括起来,尽量避免使用此函数
具体请看以下test
ex11_3.py
# -*- coding:utf-8-*-
print "what do you like?",
this =raw_input()
print"Oh,yes. I think %r is verygood!"%this
print "what do you like?",
that =input()
print"Oh,yes. I think %s is verygood!"%that
运行结果:
下面再简单测下raw_input()与input()的区别
ex11_2.py raw_input()
# -*- coding:utf-8-*-
print "How old are you?",
your_ages = raw_input()
print "How old are your brother?",
your_brother_ages = raw_input()
print"So ,your brother are older %r yearsthan you."%(your_brother_ages- your_ages)
运行结果:
ex11_2_2.py input()
# -*- coding:utf-8-*-
print "How old are you?",
your_ages = input()
print "How old are your brother?",
your_brother_ages = input()
print"So ,your brother are older %r yearsthan you."%(your_brother_ages- your_ages)
运行结果:
ex11_2_3.py input() ------实验中输出语法不对请忽视····
# -*- coding:utf-8-*-
print "How old are you?",
your_ages = input()
print "How old are your brother?",
your_brother_ages = input()
print"So ,your brother are older %r yearsthan you."%(your_brother_ages+ your_ages)
运行结果:
x = int(raw_input()) 个人理解为简单的嵌套函数
ex11_4.py
# -*- coding: utf-8-*-
print "How much money do you have?",
x =int(raw_input())
print "Oh, you have %r money."%x
运行结果: