'How do I replace() input chars in a input string?
The purpose of my program is to ask a user to input a sentence , then the user is asked to input any amount of characters they would like removed from the string. the program only replaces the last character to be input , not all of them
please keep in mind I'm fairly new at this :)
print("Please enter a sentence")
sentence = input()
print("Please type the characters you would like to make dissapear!!! ")
while True:
repchar = input()
for i in repchar:
i = sentence.replace(repchar , "")
if repchar == "":
print(i)
break
Solution 1:[1]
It was a minor error. You needed to make changes to the whole sentence, but since i changes in every iteration, the replaced sentence was never stored.
print("Please enter a sentence")
sentence = input()
print("Please type the characters you would like to make dissapear!!! ")
while True:
repchar = input()
for i in repchar:
sentence = sentence.replace(repchar , "") # here you were replacing the
# character but storing it in i
# that changes every iteration
if repchar == "":
print(sentence) # Printing the sentence not just i
break
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anshumaan Mishra |
