Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数字长度和数字的验证 a=input('hello enter something') def checkingInput(): 如果0_Python_Validation_Python 3.x - Fatal编程技术网

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/