题目: 一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------
File Name: Python练习100【030】:回文数
Author: zhangzx
Date: 2020-12-30
-------------------------------------------------------
Change Activity:
2020-03-29
-------------------------------------------------------
"""
while True:
try:
num = int(input('请您输入5位数的正整数: '))
if num>=10000 and num <=99999:
break
else:
print ('您的输入有误,请输入正整数!')
except ValueError:
print ('您的输入有误,请输入正整数!')
##方法一:
print("\n方法一的结果为:")
def palindrome(num):
string = str(num)
if string[0]==string[-1] and string[1]==string[-2]:
print(num,"是回文数!")
else:
print(num,"不是是回文数!")
palindrome(num)
# 方法二:
print("\n方法二的结果为:")
def palindrome_1(num):
num = str(num)
a = 0
b = len(num)-1
flag = True
while a<b:
if num[a]!=num[b]:
print(num,"不是回文数!")
flag = False
break
a,b = a+1,b-1
if flag:
print(num,"是回文数!")
palindrome_1(num)
---------------------------以下是输出-----------------------------
请您输入5位数的正整数: 12321
方法一的结果为:
12321 是回文数!
方法二的结果为:
12321 是回文数!