如何确保输入符合要求

时间:2013-09-26 21:25:12

标签: python if-statement input

用户必须输入密码,但密码中必须至少包含1个大写,小写和数字。

password = input("Please enter a password: ")

2 个答案:

答案 0 :(得分:4)

if any(x.isupper() for x in password) and \ 
   any(x.islower() for x in password) and \
   any(x.isdigit() for x in password):
    print ("Congratulations, you have a secure password.")

答案 1 :(得分:1)

你也可以放入一个在这些条件匹配之前不会停止的while循环:

while True:
     password = input("Please enter a password: ")
     if any(x.isupper() for x in password) and \ 
        any(x.islower() for x in password) and \
        any(x.isdigit() for x in password): #copy from Rob's awnser
            break

     else: print('Invalid!')