我的程序的目的是要求用户输入一个句子,然后要求用户输入他们希望从字符串中删除的任意数量的字符。该程序只替换最后一个要输入的字符,而不是全部替换
请记住,我在这方面还相当陌生:(
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
这是一个小错误。您需要对整个句子进行更改,但由于i
在每次迭代中都会发生更改,因此从未存储替换的句子。
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