Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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中输入或原始输入后的字符串验证_Python_String_Python 3.x_Validation_Input - Fatal编程技术网

Python中输入或原始输入后的字符串验证

Python中输入或原始输入后的字符串验证,python,string,python-3.x,validation,input,Python,String,Python 3.x,Validation,Input,我想在某些条件下测试输入的字符串。在本例中,我希望变量中的字符串至少包含一个数字字符 while any(inputted.isdigit()) == False: print ("Inputted must contain at least one numeric character") # can't figure this one out inputted = input("Please enter a combination: ") 理想的输入应该是像Boat88Sea

我想在某些条件下测试输入的字符串。在本例中,我希望变量中的字符串至少包含一个数字字符

while any(inputted.isdigit()) == False:
    print ("Inputted must contain at least one numeric character") # can't figure this one out
    inputted = input("Please enter a combination: ")

理想的输入应该是像Boat88Sea这样的字符串


任何帮助都将不胜感激

必须迭代输入字符串:

input_string = "" 
while any(c.isdigit() for c in input_string) == False:
    print ("the input string must contain at least one numerical character") 
    input_string = input("Please enter a combination: ")
当字符串中的任何字符被发现是数字时,while循环将退出

或者,这可能比您想要的更接近: 只要输入字符串中没有任何字符是数字

while not any(c.isdigit() for c in inputted):

必须迭代输入字符串:

input_string = "" 
while any(c.isdigit() for c in input_string) == False:
    print ("the input string must contain at least one numerical character") 
    input_string = input("Please enter a combination: ")
当字符串中的任何字符被发现是数字时,while循环将退出

或者,这可能比您想要的更接近: 只要输入字符串中没有任何字符是数字

while not any(c.isdigit() for c in inputted):

肯定有比这更好的方法:

cond = True
while cond:
    print ("Inputted must contain at least one numeric character") 
    inputted = input("Please enter a combination: ")
    for x in range(0,9) : 
        if str(x) in inputted : cond = False

肯定有比这更好的方法:

cond = True
while cond:
    print ("Inputted must contain at least one numeric character") 
    inputted = input("Please enter a combination: ")
    for x in range(0,9) : 
        if str(x) in inputted : cond = False

你能举个例子说明你在做什么样的投入吗?输入的典型示例是什么理想的输入是一个类似于boat88sea的字符串您能举一个您正在输入的输入类型的示例吗?一个典型的输入例子一个理想的输入应该是像boat88sea这样的字符串你不需要方括号,你可以用生成器代替列表;我更改了它-但我怀疑它有多大区别,输入不太可能很大。您不需要方括号,您可以使用生成器而不是列表。正确,谢谢;我改变了它-但我怀疑它有多大的不同,它不太可能是巨大的投入。