little tips
- ctrl+alt+L可以将杂乱的代码规整化。
- 遍历时若指定步长为-1则可以倒序遍历
- replace函数会返回一个全新的字符串,而原有字符串并不会发生什么改变。
def reverse(text):
# 获取text的倒序字符串
# 以-1为步长遍历全部字符串
return text[::-1]
def is_palindrome(text):
# 判断是否是回文数
L = [' ',',','.']
for c in L:
text = text.replace(c, "")
return text == reverse(text)
while True:
something = input("Enter text: ")
if is_palindrome(something):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")