在使用input()內置函數輸入數字時,要注意一點:
input()的返回值始終是字符串,所以type(number)永遠是<class 'str'>!
如:
>>> temp = input("請輸入一個數字:")
請輸入一個數字:3
>>> type(temp)
<class 'str'>
這一點一定要注意!!!如果接下來要使用數字時,要回用到強制類型轉化!
如下例子:
>>> temp = input("請輸入一個數字:")
請輸入一個數字:3
>>> type(temp)
<class 'str'>
>>> temp = int(temp)
>>> type(temp)
<class 'int'>
>>> temp+2
5