Strange results. There is a lab where we are to take input and alter the input.
word = input()
password = ''
i = 0
while i < len(word):
if word[i] == 'i':
password = password + '1'
elif word[i] == 'a':
password = password + '@'
elif word[i] == 'm':
password = password + 'M'
elif word[i] == 'B':
password = password + '8'
elif word[i] == 's':
password = password + '$'
else:
password == password + word[i]
i = i + 1
password = password + '!'
print(password)This code 'works' but it is dropping the items on in the while loop.
Check this out, the code below works as expected:
word = input()
password = ''
i = 0
while i < len(word):
if word[i]=='i':
password +='1'
elif word[i]=='a':
password +='@'
elif word[i]=='m':
password +='M'
elif word[i]=='B':
password +='8'
elif word[i]=='s':
password +='$'
else:
password += word[i]
i +=1
password = password + '!'
print(password)The only difference is using the shorter += way of making a variable equal to itself and the other item.
output from top code is
mypassword
M@$$!
Output from bottom code is
mypassword
Myp@$$word!
:O!!!! What gives? What am I missing?
python - How to change letters in a string entered by the user? - Stack Overflow
5.15 LAB: Password modifierMany 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 "!" to the end of the input string.ibecomes 1a becomes @mbecomes MB becomes 8s becomes $Ex: If the input is:mypasswordthe output is:Mype$$word!Hint: Python strings are immutable, but support string concatenation. Store and build the stronger password in the given password variable.553262.4201248.9×3zqy7LABACTIVITY5.15.1: LAB: Password modifier010main.pyLoad default template...1 word = input() 2 password = '' 3word = input()password =34 '.' Type your code here.
Python: Having trouble with a code for class - Stack Overflow
WGU Learn Python 6.15.1: LAB: Password modifier
You can use str.translate and str.maketrans
>>> s = 'mypassword'
>>> s.translate(s.maketrans('iamBo', '!@M8.'))
'[email protected]'
You can also harcode the shifting letters. Answers below are better but you can go like:
new_pass = ''
for char in password:
if char == 'i':
new_pass += '!'
elif char == 'a':
new_pass +='@'
elif char == 'm':
new_pass +='M'
.
.
.
else:
new_pass += char
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','.')
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)