Python 如何检查变量中是否有不允许的字符?

Python 如何检查变量中是否有不允许的字符?,python,python-3.x,Python,Python 3.x,我正在编写一个密码程序,如果满足某些要求,它将显示一个分数。但是我被困在如何检查一些字符是否是不允许的。如果不允许,则应通知读者。 这是我的密码: user_password = input("\nEnter your password:") user_score = 0 user_length = len(user_password) symbols = "$%^&*()_-+=" if len(user_password)>24: print("Your passwo

我正在编写一个密码程序,如果满足某些要求,它将显示一个分数。但是我被困在如何检查一些字符是否是不允许的。如果不允许,则应通知读者。 这是我的密码:

user_password = input("\nEnter your password:")
user_score = 0
user_length = len(user_password)
symbols = "$%^&*()_-+="

if len(user_password)>24:
    print("Your password is too long! It must be between 6 and 24")
elif len(user_password)<6:
    print("Your password is too short! It must be between 6 and 24")
elif len(user_password) >=6 and len(user_password) <= 24:
    lower = sum([int(c.islower()) for c in user_password])
    if lower > 0:
            user_score = user_score + 5
    upper = sum([int(c.isupper()) for c in user_password])
    if upper > 0:
            user_score = user_score + 5
    integer = sum([int(c.isdigit()) for c  in user_password])
    if integer > 0:
            user_score = user_score + 5
    for c in symbols:
            if c in user_password:
                    user_score = user_score + 5
    for c in user_password:
            if c not in symbols:
                    print("Some symbols you entered are not allowed")
                    break
user\u password=input(“\n输入密码:”)
用户分数=0
用户长度=len(用户密码)
symbols=“$%^&*()_957;-+=”
如果len(用户密码)>24:
打印(“您的密码太长!必须介于6和24之间”)
elif len(用户密码)=6和len(用户密码)0:
用户评分=用户评分+5
上限=总和([int(c.isupper())表示用户密码中的c])
如果上限>0:
用户评分=用户评分+5
整数=总和([int(c.isdigit())表示用户密码中的c])
如果整数>0:
用户评分=用户评分+5
对于符号中的c:
如果用户密码中有c:
用户评分=用户评分+5
对于用户密码中的c:
如果c不在符号中:
打印(“不允许输入某些符号”)
打破

我想要它,这样程序将结束,如果一个符号是错误地输入它。但是,当输入错误的符号时,它会显示信息,显示输入符号的次数。任何帮助都将不胜感激。

如果c不在符号中,请将
更改为
,如果c在符号中:

user_password = input("\nEnter your password:")
user_score = 0
user_length = len(user_password)
symbols = "$%^&*()_-+="

if len(user_password)>24:
    print("Your password is too long! It must be between 6 and 24")
elif len(user_password)<6:
    print("Your password is too short! It must be between 6 and 24")
elif len(user_password) >=6 and len(user_password) <= 24:
    lower = sum([int(c.islower()) for c in user_password])
    if lower > 0:
            user_score = user_score + 5
    upper = sum([int(c.isupper()) for c in user_password])
    if upper > 0:
            user_score = user_score + 5
    integer = sum([int(c.isdigit()) for c  in user_password])
    if integer > 0:
            user_score = user_score + 5
    for c in symbols:
            if c in user_password:
                    user_score = user_score + 5
    for c in user_password:
            if c in symbols:
                    print("Some symbols you entered are not allowed")
                    break
user\u password=input(“\n输入密码:”)
用户分数=0
用户长度=len(用户密码)
symbols=“$%^&*()_957;-+=”
如果len(用户密码)>24:
打印(“您的密码太长!必须介于6和24之间”)
elif len(用户密码)=6和len(用户密码)0:
用户评分=用户评分+5
上限=总和([int(c.isupper())表示用户密码中的c])
如果上限>0:
用户评分=用户评分+5
整数=总和([int(c.isdigit())表示用户密码中的c])
如果整数>0:
用户评分=用户评分+5
对于符号中的c:
如果用户密码中有c:
用户评分=用户评分+5
对于用户密码中的c:
如果符号中有c:
打印(“不允许输入某些符号”)
打破

希望这有帮助,欢迎来到SO

您需要更改循环
的最后一个
,以使循环失败
如果符号中有c,则有一个
not
在那里导致程序停止运行

for c in user_password:
            if c in symbols:
                    print("Some symbols you entered are not allowed")
                    break

只使用
2``行
的较短方法是使用
any

if any(c in symbols for c in user_password):
            print("Some symbols you entered are not allowed")


最后一点,您应该尝试将
缩进
宽度保持为恒定的
4
空格,以便
可读性
。我在这些代码段中没有这样做,因为您可以使用现有代码测试它们,但理想情况下您应该更改它们。

哪些字符是不允许的?回答了您的问题!希望它有帮助,如果是这样,请考虑把它标记为正确的。欢迎来到SO!你能举一个密码导致显示多条消息的例子吗?嗨,对不起,我没有在问题中详细解释。我希望这样做,以便不允许在“symbols”变量中指定任何其他字符、大写/小写和数字。此外,如果输入任何字符,如小写/大写,它会通知用户不允许使用某些符号,因为它们不在“symbols”变量中。非常感谢您的帮助。嗨,对不起,我没有充分解释我想要的是什么。基本上,变量“symbols”是允许的字符,然后我希望它是除指定的符号、大写/小写和数字之外的任何其他字符。非常感谢。