Python3中内置input()函数,键入数字时,将返回int,还是str?
例如:
>>> number = input()
6
>>> type(number)
<class 'str'>
>>> number = int(input())
6
>>> type(number)
<class 'int'>
注:input键入时,返回值为‘str’,运用时容易出错,尤其存在比较条件。
例如:
>>> a = input()
5
>>> b = input()
1
>>> a >= b
True
>>> c = b #c为‘str’
>>> a >= c
True
>>> c = 1 #c为‘int’
>>> a > c #a为‘str’,c为‘int’,不同类型不能比较
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
a > c
TypeError: '>' not supported between instances of 'str' and 'int'