Would anyone be able to help me with something simple but I seem to be running into a small snag.
I need to take an input from user for a random string. I then need to ask the user which letters they would like me to make disappear from their string.
I then need to use a for loop to make those specific letters disappear from the string and print showing the missing letters.
I have the following code so far as below:
user_input = input("Please enter a string")
disappear = input("now please enter the chracters you would like to disappear")
for a in user_input.replace(disappear, " "):
print (a)
My code runs but prints out every letter on a new line and does make my chosen letters disappear but it makes other letters disappear as well.
Any help in the simplest form will be greatly appreciated. Thank you
You can do this by replacing each individual letter:
user_input = input("Please enter a string")
disappear = input("now please enter the chracters you would like to disappear")
for a in disappear :
user_input = user_input.replace(a, '')
print(user_input)
The reason your original script wasn't working was because of this part here:
for a in user_input.replace(disappear, " "):
print (a)
What happens precisely? Well, first you evaluate:
user_input.replace(disappear, " ")
Say my user_input
is 'hello world' and dissapear
is 'he'. This would evaluate as 'llo world'. Now what happens:
for a in 'llo world':
print(a)
And you see that it should print:
l
l
o
w
o
r
l
d
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments