Python 数字长度和数字的验证 a=input('hello enter something') def checkingInput(): 如果0
Python 数字长度和数字的验证 a=input('hello enter something') def checkingInput(): 如果0,python,validation,python-3.x,Python,Validation,Python 3.x,isDigit()检查它是否为整数len()检查要比较的长度 a=input('hello enter something ') def checkingInput(): if 0 <= int(a) <= 9: return a else: print('Invalid input!') checkingInput() isDigit()有点不安全。它忽视 十六进制 小数 负数 如果您想确定您可能需要使用正则表达式并查看匹配
isDigit()
检查它是否为整数len()
检查要比较的长度
a=input('hello enter something ')
def checkingInput():
if 0 <= int(a) <= 9:
return a
else:
print('Invalid input!')
checkingInput()
isDigit()
有点不安全。它忽视
- 十六进制
- 小数
- 负数
if a.isdigit():
print("It's a digit!")
else:
print("It's not a digit!")
if len(a) == 10:
print("It's exactly 10 digits long") // digits might also mean "characters"!
else:
print("It's not exactly 10 digits long") // digits might also mean "characters"!
这也会立即检查你的长度。如果您只想检查数字,那么使用
/\A\d{10}\Z/
isDigit()
检查它是否为整数len()
检查要比较的长度
a=input('hello enter something ')
def checkingInput():
if 0 <= int(a) <= 9:
return a
else:
print('Invalid input!')
checkingInput()
isDigit()
有点不安全。它忽视
- 十六进制
- 小数
- 负数
if a.isdigit():
print("It's a digit!")
else:
print("It's not a digit!")
if len(a) == 10:
print("It's exactly 10 digits long") // digits might also mean "characters"!
else:
print("It's not exactly 10 digits long") // digits might also mean "characters"!
这也会立即检查你的长度。如果您只想检查数字,那么使用
/\A\d{10}\Z/