请教一个问题。

2016-08-11 10:14:14 +08:00
 getlost
最近开始学 python ,以前没怎么接触编程,就大二的时候有学过 vb 。
----------------------------------------------------------------------------------------------------------------------------------------------
def number_generator():
n = 11
while True:
yield n
n = n + 1


def palindrome_check(n):
string = str(n)
return string[0] == string[::-1]


def next_number():
it = number_generator()
while True:
n = next(it)
yield n
it = filter(palindrome_check(n), it)


for a in next_number():
if a < 1000:
print(a)
else:
break
----------------------------------------------------------------------------------------------------------------------------------------------
这是判断回数的代码,但是一直出错
----------------------------------------------------------------------------------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python35-32/HH.py", line 21, in <module>
for a in huishu():
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python35-32/HH.py", line 16, in huishu
n = next(it)
TypeError: 'bool' object is not callable
----------------------------------------------------------------------------------------------------------------------------------------------请问问题在哪啊(为什么没排版??????)
2552 次点击
所在节点    Python
11 条回复
gotounix
2016-08-11 11:40:29 +08:00
next_number 函数中
it = filter(palindrome_check, it)
getlost
2016-08-11 11:52:59 +08:00
@gotounix 有什么什么问题啊,不能调用吗?
mgna17
2016-08-11 12:01:11 +08:00
发帖的时候右下角选 markdown
代码这样贴:

```
你的代码
```
getlost
2016-08-11 12:03:23 +08:00
@mgna17 好的,初来乍到,还不熟悉,谢谢
hitmanx
2016-08-11 12:08:07 +08:00
Filter 的第一个变量应该传个函数指针,你传的的是个函数调用,类型相当于是它的返回值,是 bool 的,所以说 bool not callable 。另外你是不是忘了把 int 转成 str 了,当调用 palindrome 时
gotounix
2016-08-11 12:15:23 +08:00
@getlost 写错了,怎么调用?
lll9p
2016-08-11 12:57:32 +08:00
```
def number_generator():
n = 11
while True:
yield n
n = n + 1


def palindrome_check(n):
string = str(n)
# ....
return string == string[::-1]


def next_number():
it = number_generator()
while True:
n = next(it)
yield n
# ....
it = filter(palindrome_check, it)


for a in next_number():
if a < 1000:
print(a)
else:
break
```
lll9p
2016-08-11 12:58:35 +08:00
palindrome_check 不是应该用 string == string[::-1] 来判断的吗
还有应该写成这样 filter(palindrome_check, it)
getlost
2016-08-11 14:39:06 +08:00
@hitmanx 可以了,谢谢
、、、
def palindrome_check(n):
string = str(n)
return string == string[::-1]
、、、
这里转了啊
getlost
2016-08-11 14:39:43 +08:00
@lll9p 谢谢,可以了
aitaii
2016-08-11 18:36:05 +08:00
``` python

def palindrome_check(n):
string = str(n)
return string == string[::-1]

```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/298562

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX