创新互联Python教程:python中input输入为数字

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

方法一:使用eval()函数,输入的持续为数字

def inputInt(content='请输入整数:'):
    while True:
        data = input(content)
        try:
            inputData = eval(data)
            if type(inputData) == int:
                # break
                return inputData
        except:
            pass
 
 
if __name__ == "__main__":
    print(inputInt())

方法二、使用int()转换

while True:
    ten=None
    try:
        ten=int(input("x:"))
    except:
        pass
    if type(ten)==int:
        break

以上就是python中input输入为数字的两种方法,大家可以尝试看看哦~


网站栏目:创新互联Python教程:python中input输入为数字
网页网址:http://zjjierui.cn/article/cdhpsdi.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款