Write a program that prompts the user to enter an integer and reports whether the integer is a palindrome. A number is a palindrome if its reversal is the same as itself. Here are two sample runs: Enter an integer: 121 121 is a palindrome. Enter an integer:123 123 is not a palindrome.
时间: 2024-02-14 21:14:09 浏览: 127
Sure, here's a Python program that does what you're asking for:
```python
# Prompt the user to enter an integer
num = input("Enter an integer: ")
# Check if the integer is a palindrome
if num == num[::-1]:
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")
```
Here's an explanation of how the program works:
1. We prompt the user to enter an integer using the `input()` function, and store the result in the variable `num`.
2. We check if `num` is equal to its reversal, which we can obtain by using the slicing operator `[::-1]` to reverse the string. If `num` is equal to its reversal, we print a message saying that it's a palindrome. Otherwise, we print a message saying that it's not a palindrome.
Note that we're treating the input as a string, since we want to be able to reverse it easily using string slicing. If we wanted to treat it as an integer instead, we would need to convert it using `int(num)` before reversing it.
阅读全文