1. 程式人生 > >Python3學習筆記:input()函式的返回值

Python3學習筆記:input()函式的返回值

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'