py3的input
- input() 是内置函数,从控制台读取用户输入的内容。
- 总是以字符串来处理用户输入,所以用户输入的内容可以包含任何字符。
- str = input(tipmsg)
- str 表示一个字符串类型的变量,input 会将读取到的字符串放入 str 中。
- tipmsg 提示信息,显示在控制台上,
- 告诉用户应该输入什么样的内容;如果不写 tipmsg,就不会有任何提示信息。
a = input("Enter a number: ")
b = input("Enter another number: ")
print("aType: ", type(a))
print("bType: ", type(b))
result = a + b
print("resultValue: ", result)
print("resultType: ", type(result))
- 运行结果
Enter a number: 100↙
Enter another number: 45↙
aType: <class ‘str’>
bType: <class ‘str’>
resultValue: 10045
resultType: <class ‘str’>
↙表示按下回车键,按下回车键后 input() 读取就结束了。
- Python只是它们当成了字符串,+起到了拼接字符串的作用,而不是求和的作用。
- 内置函数将字符串转换成想要的类型,如:
- int(string) 将字符串转换成 int 类型;
- float(string)
- bool(string)
a = input("Enter a number: ")
b = input("Enter another number: ")
a = float(a)
b = int(b)
print("aType: ", type(a))
print("bType: ", type(b))
result = a + b
print("resultValue: ", result)
print("resultType: ", type(result))
- 结果:
Enter a number: 12.5↙
Enter another number: 64↙
aType: <class ‘float’>
bType: <class ‘int’>
resultValue: 76.5
resultType: <class ‘float’>
参考链接
- http://c.biancheng.net/view/4228.html