Checking for characters

I'm having trouble coding this. I'm learning swift via develop with swift by Apple, and when I try to code this, it stop at "Please add a digit." Can someone explain what I'm doing wrong? Thanks!

let tenMostCommonPasswords = [

    "123456",

    "password",

    "12345678",

    "qwerty",

    "12345",

    "123456789",

    "letmein",

    "1234567",

    "football",

    "iloveyou"

]

let digits = "0123456789"

let punctuation = "!@#$%^&*(),.<>;'`~[]{}\|/?_-+= "

let password = "Pasword12!"

for _ in tenMostCommonPasswords{

   if tenMostCommonPasswords.contains(password){

    print("Please change your password")

   }

   else{

    for _ in digits{

        if digits.contains(password){

            for _ in punctuation{

                if punctuation.contains(password){

                    print("You're good to go!")

                }

                else{

                    print("Please add a punctuation")

                }

            }

        }

        else{

            print("Please add a digit.")

        }

    }

   }

}

Answered by OOPer in 683640022

When you show your code, please use the Code Block feature of this site:

(And a good indentation would make the code far more readable, Ctrl-I in Xcode editor will do it for you.)

let tenMostCommonPasswords = [
    "123456",
    "password",
    "12345678",
    "qwerty",
    "12345",
    "123456789",
    "letmein",
    "1234567",
    "football",
    "iloveyou"
]
let digits = "0123456789"
let punctuation = #"!@#$%^&*(),.<>;'`~[]{}\|/?_-+= "#
let password = "Pasword12!"
for _ in tenMostCommonPasswords {
    if tenMostCommonPasswords.contains(password) {
        print("Please change your password")
    }
    else {
        for _ in digits {
            if digits.contains(password) {
                for _ in punctuation {
                    if punctuation.contains(password) {
                        print("You're good to go!")
                    }
                    else {
                        print("Please add a punctuation")
                    }
                }
            }
            else {
                print("Please add a digit.")
            }
        }
    }
}

The expression digits.contains(password) gets true when digits("0123456789") contains the string password ("Pasword12!"). But as you see, "0123456789" does not contain "Pasword12!". So the else-close will be executed.

I guess you may want to do something like this:

if tenMostCommonPasswords.contains(password) {
    print("Please change your password")
}
else {
    for digitChar in digits {
        if password.contains(digitChar) {
            for punctuationChar in punctuation {
                if password.contains(punctuationChar) {
                    print("You're good to go!")
                }
                else {
                    print("Please add a punctuation")
                }
            }
        }
        else {
            print("Please add a digit.")
        }
    }
}

You may find this code needs to be fixed a little more, but it might be a good habit to leave a room for learning by yourself.

Accepted Answer

When you show your code, please use the Code Block feature of this site:

(And a good indentation would make the code far more readable, Ctrl-I in Xcode editor will do it for you.)

let tenMostCommonPasswords = [
    "123456",
    "password",
    "12345678",
    "qwerty",
    "12345",
    "123456789",
    "letmein",
    "1234567",
    "football",
    "iloveyou"
]
let digits = "0123456789"
let punctuation = #"!@#$%^&*(),.<>;'`~[]{}\|/?_-+= "#
let password = "Pasword12!"
for _ in tenMostCommonPasswords {
    if tenMostCommonPasswords.contains(password) {
        print("Please change your password")
    }
    else {
        for _ in digits {
            if digits.contains(password) {
                for _ in punctuation {
                    if punctuation.contains(password) {
                        print("You're good to go!")
                    }
                    else {
                        print("Please add a punctuation")
                    }
                }
            }
            else {
                print("Please add a digit.")
            }
        }
    }
}

The expression digits.contains(password) gets true when digits("0123456789") contains the string password ("Pasword12!"). But as you see, "0123456789" does not contain "Pasword12!". So the else-close will be executed.

I guess you may want to do something like this:

if tenMostCommonPasswords.contains(password) {
    print("Please change your password")
}
else {
    for digitChar in digits {
        if password.contains(digitChar) {
            for punctuationChar in punctuation {
                if password.contains(punctuationChar) {
                    print("You're good to go!")
                }
                else {
                    print("Please add a punctuation")
                }
            }
        }
        else {
            print("Please add a digit.")
        }
    }
}

You may find this code needs to be fixed a little more, but it might be a good habit to leave a room for learning by yourself.

Checking for characters
 
 
Q