Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Taking online python course and need to make a password, aka the input, more complex

I’m currently in the process of learning python and having a lot of trouble with the syntax.
The problem I’m currently working on is asking me to make a simple password/input more complex. So far I’m trying to use a for loop to cycle through all the letters and see if they need to be changed, as in m -> M. I want each loop to add the resulting character onto a new variable(new_pass) and basically put the characters together to make the new password. That’s the thought anyways. The sample input I’m working off is the word ‘mypassword’, which in turn should output ‘Myp@$$word!’ per the if/elif statements below.
Currently I’m getting an error at line 6 "invalid syntax". Thanks in advance if you can help me out!

word = input()
password = ''
new_pass = ''

for letter in word:
    if letter == i
        letter = 1
    elif letter == a:
        letter = @
    elif letter == m:
        letter = M
    elif letter == B:
        letter = 8
    elif letter == s:
        letter = $
    else:
    new_pass = new_pass + letter

print(new_pass)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First, you must have quotes around your strings. For example, instead of letter = @, you must do letter = '@'.

Secondly, it is a better approach to use a dictionary of replacement letters and loop over the list checking for the replacements.You can do it like this:

word = input()

replacements = {'i': '1', 'a': '@', 'm': 'M', 'B': '8', 's': '$'}
new_pass = ''
for letter in word:
    if letter in replacements:
        new_pass += replacements[letter]
    else:
        new_pass += letter

print(new_pass)

You can also do this with a list comprehension if you like:

word = input()

replacements = {'i': '1', 'a': '@', 'm': 'M', 'B': '8', 's': '$'}
new_pass = ''.join([replacements[l] if l in replacements else l for l in word])

print(new_pass)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading