Reputation: 73
Prompt: Many user-created passwords are simple and easy to guess. Write a program that takes a simple password and makes it stronger by replacing characters using the key below, and by appending "q*s" to the end of the input string.
i becomes !
a becomes @
m becomes M
B becomes 8
o becomes .
Ex:
If the input is:
mypassword
the output is:
[email protected]*s
What I have so far:
#assign variables
word = input()
password = ''
#checking for and modifying user's password if containing the characters: i, a, m, B, or o
for character in word:
if(character=='i'): #if user's password contains the letter i change to !
word += '!'
elif(character=='a'): #if user's password contains the letter a change to @
word += '@'
elif(character=='m'): #if the user's password contains the letter m change to M
word += 'M'
elif(character=='B'): #if the user's password contains the letter B change to 8
word += '8'
elif(character=='o'): #if the user's password contains the letter o change to .
word += '.'
password = (word + 'qs')
#output final password
print(password)
My output is messed up. I input the password as mypassword and the output is [email protected]
Can someone please help me by telling me where I'm messing up?
Upvotes: 1
Views: 36562
Reputation: 1
I had the same question in my studies, and it was designed to test your knowledge in loops. While there are simpler ways of coding this, I feel this may be more along the lines of what the exercise is looking for.
word = input()
password = ''
for character in word: #Begin looping through string
if character == 'i': #If 'i' is present, replace with '!'
word = word.replace('i', '!')
elif character == 'a': #Else If 'a' is present, replace with '@'
word = word.replace('a', '@')
elif character == 'm': #Else If 'm' is present, replace with 'M'
word = word.replace('m', 'M')
elif character == 'B': #Else If 'B' is present, replace with '8'
word = word.replace('B', '8')
elif character == 'o': #Else If 'o' is present, replace with '.'
word = word.replace('o', '.')
else: #Changes have been made, append 'q*s'
word += 'q*s'
password = word #Set new password
print(password)
Upvotes: 0
Reputation: 1
word = input()
password = ''
# Modify characters i, a, m, B, and o in password
for character in word:
if character == 'i': # IF character equals 'i' change to '!'
password += '!'
elif character == 'a': # ELSE IF character equals 'a' change to '@'
password += '@'
elif character == 'm': # ELSE IF character equals 'm' change to 'M'
password += 'M'
elif character == 'B': # ELSE IF character equals 'B' change to '8'
password += '8'
elif character == 'o': # ELSE IF character equals 'o' change to '.'
password += '.'
else: # When symbols do not get replaced
password += character
print(password + 'q*s') # Make password stronger by adding "q*s" to the end of
# the input string
Upvotes: 0
Reputation: 9481
Following your original logic, you need to initialise a word to return, otherwise all characters get appended to your old password.
word = input()
password = ''
#checking for and modifying user's password if containing the characters: i, a, m, B, or o
for character in word: # update password rather than word
if(character=='i'): #if user's password contains the letter i change to !
password += '!'
elif(character=='a'): #if user's password contains the letter a change to @
password += '@'
elif(character=='m'): #if the user's password contains the letter m change to M
password += 'M'
elif(character=='B'): #if the user's password contains the letter B change to 8
password += '8'
elif(character=='o'): #if the user's password contains the letter o change to .
password += '.'
else: # add else statement for when symbols do not get replaced
password += character
print(password)
Upvotes: 0
Reputation: 2075
You can use .replace()
word = 'mypassword'
word = word.replace('i','!')
word = word.replace('a','@')
word = word.replace('m','M')
word = word.replace('B','8')
word = word.replace('o','.')
word = word + 'q*s'
print(word)
>>> [email protected]*s
You can also put all the .replace()
on one line,
word = word.replace('i','!').replace('a','@').replace('m','M').replace('B','8').replace('o','.')
Upvotes: 2