Python 用户输入
函数 inpt()工作原理
函数 inpt() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,将值存储到一个变量中,值得类型为字符串
message = input("Tell me something, and I will repeat it back to you:")
print("===>>:%s"%(message))
Tell me something, and I will repeat it back to you:Hello everyone!
===>>:Hello everyone!
程序提示语句要清晰
使用input()函数时,需要指出用户需要输入的信息内容
name = input("Please enter your name :")
print("===>>:Hello %s !"%(name))
Please enter your name :jorbabe
===>>:Hello jorbabe !
input()获取数值输入
age = input("How old are you :")
print("===>>: %s type:%s "%(age,type(age)))
How old are you :15
===>>: 15 type:<class 'str'>
转换输入的数字类型int
age = input("How old are you :")
age = int(age)
print("===>>: %s type:%s "%(age,type(age)))
How old are you :15
===>>: 15 type:<class 'int'>
求模运算符
处理数值信息时,求模运算符(%)是一个很常用的工具,将两个数值相除并返回余数
age = input("Enter a number, and I'll tell you if it's even or odd:")
age = int(age)
print("===>>: %s type:%s "%(age,type(age)))
if age % 3 == 0 :
print(" The number %s is even ."%(age))
else:
print(" The number %s is odd ."%(age))
Enter a number, and I'll tell you if it's even or odd:15
===>>: 15 type:<class 'int'>
The number 15 is even .
Enter a number, and I'll tell you if it's even or odd:16
===>>: 16 type:<class 'int'>
The number 16 is odd .
Python 2.7 获取输入
在Python 2.7中输入模块使用 raw_input()
2.7中的input() 模块解读输入内容为运行代码,请勿使用