简体   繁体   English

如何将输入与 Python 中列表中的字符串进行比较?

[英]How do I compare an input to strings in a list in Python?

I want to create a password program that checks to see if a special character is inside an input.我想创建一个密码程序来检查输入中是否有特殊字符。

So I created a list with some special characters so that if just one or multiple symbols are detected in the input, then it should do something:所以我创建了一个包含一些特殊字符的列表,这样如果在输入中只检测到一个或多个符号,那么它应该做一些事情:

SpecialSymbols =['$', '@', '#', '%']
Password = input("type password")

if Password in SpecialSymbols:
    print("strong password")
else:
    print("no special character detected")

I think I need to use a for loop as well in which case it prints all items.我想我也需要使用 for 循环,在这种情况下它会打印所有项目。 Or I may need to check just specific characters and not the entire input, how do I do this?或者我可能只需要检查特定字符而不是整个输入,我该怎么做?

You can use any (if there should be at least one special char from the list) or all if there must be all special chars from the list in the input string.如果输入字符串中的列表中必须有所有特殊字符,则可以使用any (如果列表中至少应该有一个特殊字符)或all

if any(x in SpecialSymbols for x in Password):
if all(x in SpecialSymbols for x in Password):

See a Python demo :请参阅Python 演示

SpecialSymbols =['$', '@', '#', '%']
Passwords = ["String #$%@", "String #1", "String"]
for Password in Passwords:
    if any(x in SpecialSymbols for x in Password):
        print("strong password")
    else:
        print("no special character detected")

Output for this snippet:此代码段的 Output :

strong password
strong password
no special character detected

Loop through the string遍历字符串

SpecialSymbols =['$', '@', '#', '%']
Password = input("type password")
strong = False
for c in Password:
    if c in SpecialSymbols:
        strong = True
        break
if strong:
    print("Password is strong")
else:
    print('password is weak')

This code detects how many of the special characters are in the password.此代码检测密码中有多少特殊字符。

SpecialSymbols =['$', '@', '#', '%']
Password = input("type password")

number = 0
for symbol in SpecialSymbols: # for each symbol
    if symbol in Password: # check if it is in the password
        number += 1

if number >= 1: # symbol detected in the password
    print("strong password (%d special symbols in)" % number)
else:
    print("no special character detected")

More compact: only check if there is any symbol, not using a counter of occurrences but a boolean strong value:更紧凑:只检查是否有任何符号,不使用出现计数器,而是使用 boolean strong值:

strong = False
for symbol in SpecialSymbols:
    if symbol in Password:
        strong = True
        break

if strong:
    print("strong password")
else:
    print("no special character detected")

To solve your problem, I have created a function that is going to check for each letter in the word if it is a special character.为了解决您的问题,我创建了一个 function 来检查单词中的每个字母是否是特殊字符。 If it is, then it is going to higher the score and return true.如果是,那么它将提高score并返回 true。 Then you can use this function for the input password.然后你可以使用这个 function 来输入密码。

Here is the code:这是代码:

SpecialSymbols =['$', '@', '#', '%']
Password = input("type password")

def strongPassword(password):
    score = 0
    for letter in Password:
        for spec in SpecialSymbols:
            if letter == spec :
                score = score + 1
    print(score)
    if score : 
        return True

strongPassword(Password)

if strongPassword(Password):
    print("strong password")
else:
    print("no special character detected")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM