首页 > 代码库 > 简明Python教程笔记(二)----用户交互raw_input()
简明Python教程笔记(二)----用户交互raw_input()
raw_input()
python内建函数
将所有输入看做字符串,返回字符串类型
input()对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )
input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数
例子:
#!/usr/bin/env python
this_year = 2014
name = raw_input(‘please input your name:‘)
age1 = raw_input("how old are you?")
age = int(raw_input(‘how old are you?‘)) #将字符串型转为int型
print "hello",name,‘\n‘
print "you are",age1,‘years old!‘
print "you are",age,‘years old!‘
print "so you were born in ", this_year - age
print "so you were born in ", this_year - age1
脚本执行结果:
C:\Users\d\Desktop>python jiaohu.py
please input your name:cuijuntao
how old are you?25
how old are you?26
hello cuijuntao
you are 25 years old!
you are 26 years old!
so you were born in 1988
so you were born in
Traceback (most recent call last):
File "jiaohu.py", line 11, in <module>
print "so you were born in ", this_year - age1
TypeError: unsupported operand type(s) for -: ‘int‘ and ‘str‘
因为age1不是int型,无法做算法运算而报错。
本文出自 “1058223494” 博客,请务必保留此出处http://4708705.blog.51cto.com/4698705/1430055