為何 isinstance
temp = input(" 不妨猜一下小甲魚現在心里想的是哪個數字: ")
while not isinstance(temp, int):
? ? print(" 抱歉,輸入不合法, ", end='')?
? ? temp = input(" 請輸入一個整數: ")
為何輸入什么數字他都是 在while循環當中
temp = input(" 不妨猜一下小甲魚現在心里想的是哪個數字: ")
while not isinstance(temp, int):
? ? print(" 抱歉,輸入不合法, ", end='')?
? ? temp = input(" 請輸入一個整數: ")
為何輸入什么數字他都是 在while循環當中
2019-05-24
舉報
2019-05-26
在python3.x中raw_input( )和input( )進行了整合,去除了raw_input( ),僅保留了input( )函數,其接收任意任性輸入,將所有輸入默認為字符串處理,并返回字符串類型。
所以,不管你輸入的什么,isinstance都會返回false,然后再not變為true.那么永遠是死循環。故應該將輸入的數據進行轉換。
參考代碼:
temp = eval(input(" 不妨猜一下小甲魚現在心里想的是哪個數字: "))
while?not?isinstance(temp,?int):
????print("?抱歉,輸入不合法,?",?end='')
????temp?=?input("?請輸入一個整數:?")