3

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. Or I may need to check just specific characters and not the entire input, how do I do this?

Altaic
  • 45
  • 5
  • 1
    `if all(x in SpecialSymbols for x in Password): print("strong password")`? or `if any(x in SpecialSymbols for x in Password): print("strong password")`? – Wiktor Stribiżew Apr 18 '21 at 10:29
  • 1
    Did you mean `if SpecialSymbols in Password:`? – D_00 Apr 18 '21 at 10:30
  • 1
    `strong = False` `for symbol in SpecialSymbols:if symbol in Password:strong=True` `if strong:print(strong password)` - Difficult to print several lines in one line, tell me if it isn't clear for you. – D_00 Apr 18 '21 at 10:31

4 Answers4

3

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.

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

See a Python demo:

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:

strong password
strong password
no special character detected
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
3

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')
Sid
  • 2,174
  • 1
  • 13
  • 29
2

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:

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

if strong:
    print("strong password")
else:
    print("no special character detected")
D_00
  • 1,440
  • 2
  • 13
  • 32
2

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. If it is, then it is going to higher the score and return true. Then you can use this function for the input password.

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")
Sulay35
  • 21
  • 4