折腾:
期间,需要Python 3中判断input进来的字符串,是否是数字,整型或浮点数
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
有个isdigit
How do I check if an input is an integer or not in python? – Stack Overflow
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,输出:
请输入数值:3 inputStr=3 isDigit=True isDecimal=True isNumeric=True
输入:3.14
请输入数值:3.14 inputStr=3.14 isDigit=False isDecimal=False isNumeric=False
->说明:
此处,只能用isdecimal,判断是否是:int整数
无法判断是否是浮点数
不过干脆就不用了,还是去换用:int和float强制转换吧
# 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
请输入数值:3 inputStr=3 -> intValue=3 输入的字符串是浮点数或整数
浮点数:3.14
请输入数值: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' 您输入的不是数字,请重新输入!
即可。