Discuss / Python / 练习:哪里写错了,找不到问题出在哪了??谁给指点下。。。。

练习:哪里写错了,找不到问题出在哪了??谁给指点下。。。。

Topic source

暗影之_刃

#1 Created at ... [Delete] [Delete and Lock User]

import math

def _fn_num(): n=1 while True: yield n n=n+1

def _fn_filter(n): i=0 ls=str(n) L=len(ls) while i <= math.ceil(L/2): if ls[i] != ls[-(i+1)]: return False else: i=i+1 return True

def is_palindrome(): numList=_fn_num() while True: n = next(numList) yield next(filter(_fn_filter(n),numList))

def show(): ind = 1
for x in is_palindrome(): ind=ind+1 print(x) if ind > 1000: break show()

暗影之_刃

#2 Created at ... [Delete] [Delete and Lock User]

找到问题了,个位数的不用做处理的,只需添加个判断就行了

暗影之_刃

#3 Created at ... [Delete] [Delete and Lock User]

修改后的代码: import math

def _fn_num(): n=1 while True: yield n n=n+1

def _fn_filter(n): i=0 ls=str(n) L=len(ls) if L == 1: return True while i <= math.ceil(L/2): if ls[i] != ls[-(i+1)]: return False else: i=i+1 return True

def is_palindrome(): numList=_fn_num() while True: n = next(numList) yield next(filter(_fn_filter,numList))

def show(): ind = 1
for x in is_palindrome(): ind=ind+1 print(x) if ind > 10: break show()


  • 1

Reply