新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python里面如何輸入數(shù)字
要求:用python寫一個要求用戶輸入數(shù)字,如果不是數(shù)字就一直循環(huán)要求輸入,直到輸入數(shù)字為止的代碼

成都創(chuàng)新互聯(lián)是一家專業(yè)提供泌陽企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、網(wǎng)站制作、HTML5、小程序制作等業(yè)務(wù)。10年已為泌陽眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
錯誤打開方式:
while True:
ten=input('Enter a number:')
if type(eval(ten))==type(int):
break
用這個輸入字母可以可是輸入字母就直接報錯中斷了
推薦學(xué)習(xí)《python教程》。
主要出在eval上。
第一個方案:
while True:
ten=input("x:")
try:
x=eval(ten)
if type(x)==int:
break
except:
pass
然后輸入asf,沒有提示。輸入344就退出了
x:asf x:344
第二個方案
while True:
ten=None
try:
ten=int(input("x:"))
except:
pass
if type(ten)==int:
break
這個在python3.0上調(diào)試通過
第三個方案:python2.x的方案
while True:
ten=raw_input("Enter a number:")
if ten.isdigit():
break
ten=int(ten) #或者是ten=eval(ten) 網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:python里面如何輸入數(shù)字
鏈接分享:http://www.fisionsoft.com.cn/article/dpscpdd.html


咨詢
建站咨詢
