[python]代码库
import sys
def palindrome_num(n):
# reversed()返回的是一个对象,需转换成序列
n1 = ''.join(reversed(n))
s = "是回文数" if str(n) == n1 else "不是回文数"
# n1 = list(reversed(n))
# s = "是回文数" if list(n) == n1 else "不是回文数"
# n1 = tuple(reversed(n))
# s = "是回文数" if tuple(n)==n1 else "不是回文数"
print(n + s)
def isValid(msg):
# 若输入的不是整数或者不是5位数 # 输出:“输入不合法!”,并不返回任何值
# 法二:
# return 1 if msg.isnumeric() and len(msg) == 5 else "输入不合法!"
if msg.isnumeric() and len(msg) == 5:
return 1
else: # 若输入的不是整数或者不是5位数
print("输入不合法!") # 输出:“输入不合法!”,并不返回任何值
if __name__ == '__main__':
while True:
num = input("请输入一个5位数:")
if num == '0':
sys.exit()
if isValid(num) is None: # 若返回值为None,即输入不合法,重新输入
continue
else: # 否则调用palindrome_num()判断num是否是回文数
palindrome_num(num)
# 法二:
# if isValid(num) == 1:
# palindrome_num(num)
# else:
# print(isValid(num))
# continue