# 【已解决】Python中如何判断input输入的是整型或浮点数的数字

Python 7697浏览

【问答】python入门编程求解

python input check not int

Python Check User input is a Number or String | Accept Numbers as input

python – How to check if string input is a number? – Stack Overflow

if statement – How do I check if input is a number in Python? – Stack Overflow

How do I check if an input is an integer or not in python? – Stack Overflow

python isdigit

Python isdigit()方法 | 菜鸟教程

Built-in Types — Python 3.8.2 documentation

str.isdecimal()

Return True if all characters in the string are decimal characters and there is at least one character, False otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.

str.isdigit()

Return True if all characters in the string are digits and there is at least one character, Falseotherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

```inputStr = input('请输入数值：')
if not inputStr:
# 输入的是空字符串
print("input is empty")

```

print(“inputStr=%s” % inputStr)

isDigit = inputStr.isdigit()

print(“isDigit=%s” % isDigit)

isDecimal = inputStr.isdecimal()

print(“isDecimal=%s” % isDecimal)

isNumeric = inputStr.isnumeric()

print(“isNumeric=%s” % isNumeric)

```请输入数值：3
inputStr=3
isDigit=True
isDecimal=True
isNumeric=True```

```请输入数值：3.14
inputStr=3.14
isDigit=False
isDecimal=False
isNumeric=False```

->说明：

```# Function: demo for
#   python 入门编程求解-CSDN论坛
#   https://bbs.csdn.net/topics/396432611
# Author: Crifan Li
# Update: 20200419

inputStr = input('请输入数值：')
if not inputStr:
# 输入的是空字符串
print("input is empty")

# print("inputStr=%s" % inputStr)
# isDigit = inputStr.isdigit()
# print("isDigit=%s" % isDigit)
# isDecimal = inputStr.isdecimal()
# print("isDecimal=%s" % isDecimal)
# isNumeric = inputStr.isnumeric()
# print("isNumeric=%s" % isNumeric)

isInt = False
isFloat = False

intValue = None
try:
intValue = int(inputStr)
isInt = True
print("inputStr=%s -> intValue=%s" % (inputStr, intValue))
except ValueError as intErr:
print("Convert %s to int error: %s" % (inputStr, intErr))

# 如果是int整数就没必要再去检测是否是浮点数了
if not isInt:
floatValue = None
try:
floatValue = float(inputStr)
isFloat = True
print("inputStr=%s -> floatValue=%s" % (inputStr, floatValue))
except ValueError as floatErr:
print("Convert %s to float error: %s" % (inputStr, floatErr))

# inputStrType = type(inputStr) # <class 'str'>
# inputValue = eval(inputStr)

# # 检测变量类型 推荐用 方式1：isinstance
# isFloat = isinstance(inputValue, float) # 输入字符串类型是浮点类型
# isInt = isinstance(inputValue, int) # 输入字符串类型是整数类型

# # 检测变量类型 方式2：type
# inputValueType = type(inputValue) # <class 'int'>
# isFloat = inputValueType == float # 输入字符串类型是浮点类型
# isInt = inputValueType == int # 输入字符串类型是整数类型

if isFloat or isInt:
print("输入的字符串是浮点数或整数")
else:
print("您输入的不是数字，请重新输入！")```

```请输入数值：3
inputStr=3 -> intValue=3

```请输入数值：3.14
Convert 3.14 to int error: invalid literal for int() with base 10: '3.14'
inputStr=3.14 -> floatValue=3.14

```请输入数值：abc
Convert abc to int error: invalid literal for int() with base 10: 'abc'
Convert abc to float error: could not convert string to float: 'abc'

82 queries in 0.170 seconds, using 22.05MB memory