• 技术文章 >Python技术 >Python基础教程

    python中input输入为数字

    宋雪维宋雪维2021-02-21 09:27:14原创12214

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

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    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()转换

    1

    2

    3

    4

    5

    6

    7

    8

    while True:

        ten=None

        try:

            ten=int(input("x:"))

        except:

            pass

        if type(ten)==int:

            break

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

    专题推荐:pythoninput输入数字
    上一篇:如何在python中使用bytearray函数? 下一篇:python unichr() 函数用法

    相关文章推荐

    • python中使用slice函数• python中print函数输出时sep与end的区别• python中memoryview()函数是什么• python中next函数如何使用?• 如何在python中使用bytearray函数?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网