var1 = int(input("enter a number:"))
num = str(var1)[::-1]
if var1 == num:
print("its a palindrome")
else:
print("its not a palindrome")
enter a number:121
---> 预期“它是一个回文”
its not a palindrome
enter a number:123
its not a palindrome
为什么第一个示例中的代码不起作用?
比较时,将 var1 转换为字符串。见下图:
var1 = int(input("enter a number:"))
num = str(var1)[::-1]
if str(var1) == num:
print("its a palindrome")
else:
print("its not a palindrome")