Skip to main content
 首页 » 编程设计

python之输入后的字符串验证或 Python 中的 raw_input

2025年05月04日44shihaiming

我想根据特定条件测试输入的字符串。在这种情况下,我希望变量中的字符串至少包含一个数字字符...

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):