我正在尝试删除空格。
我已经尝试了以前的所有线程,包括re.sub
代码:
wordinput = (input("Input:\n"))
wordinput = wordinput.lower()
cleanword = wordinput.replace(" ","")
cleanword = wordinput.replace(",","")
cleanword = wordinput.replace(".","")
revword = cleanword [::-1]
print(cleanword)
print(revword)
print("Output:")
if (cleanword == revword):
print('"The word ' + wordinput + ' is a palindrome!"')
else:
print('"Unfortunately the word ' + wordinput + ' is not a palindrome. :(')
输出:
Input:
mr owl ate my metal worm
mr owl ate my metal worm
mrow latem ym eta lwo rm
Output:
"Unfortunately the word mr owl ate my metal worm is not a palindrome. :(
@StephenRauch explains你的问题解决得很好。你知道吗
但这里有一个更好的方法来实现你的逻辑:
您遇到的问题是:
您没有保存上一次替换的结果。你知道吗
尝试:
你有没有试过这样的方法:
相关问题 更多 >
编程相关推荐