V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
getlost
V2EX  ›  Python

请教一个问题。

  •  
  •   getlost · 2016-08-11 10:14:14 +08:00 · 2528 次点击
    这是一个创建于 2787 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近开始学 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
    ----------------------------------------------------------------------------------------------------------------------------------------------请问问题在哪啊(为什么没排版??????)
    第 1 条附言  ·  2016-08-11 11:54:10 +08:00
    有一个错误,应该这样
    def palindrome_check(n):
    string = str(n)
    return string == string[::-1]
    11 条回复    2016-08-11 18:36:05 +08:00
    gotounix
        1
    gotounix  
       2016-08-11 11:40:29 +08:00
    next_number 函数中
    it = filter(palindrome_check, it)
    getlost
        2
    getlost  
    OP
       2016-08-11 11:52:59 +08:00
    @gotounix 有什么什么问题啊,不能调用吗?
    mgna17
        3
    mgna17  
       2016-08-11 12:01:11 +08:00
    发帖的时候右下角选 markdown
    代码这样贴:

    ```
    你的代码
    ```
    getlost
        4
    getlost  
    OP
       2016-08-11 12:03:23 +08:00
    @mgna17 好的,初来乍到,还不熟悉,谢谢
    hitmanx
        5
    hitmanx  
       2016-08-11 12:08:07 +08:00 via iPhone
    Filter 的第一个变量应该传个函数指针,你传的的是个函数调用,类型相当于是它的返回值,是 bool 的,所以说 bool not callable 。另外你是不是忘了把 int 转成 str 了,当调用 palindrome 时
    gotounix
        6
    gotounix  
       2016-08-11 12:15:23 +08:00
    @getlost 写错了,怎么调用?
    lll9p
        7
    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
        8
    lll9p  
       2016-08-11 12:58:35 +08:00
    palindrome_check 不是应该用 string == string[::-1] 来判断的吗
    还有应该写成这样 filter(palindrome_check, it)
    getlost
        9
    getlost  
    OP
       2016-08-11 14:39:06 +08:00
    @hitmanx 可以了,谢谢
    、、、
    def palindrome_check(n):
    string = str(n)
    return string == string[::-1]
    、、、
    这里转了啊
    getlost
        10
    getlost  
    OP
       2016-08-11 14:39:43 +08:00
    @lll9p 谢谢,可以了
    aitaii
        11
    aitaii  
       2016-08-11 18:36:05 +08:00
    ``` python

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

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4729 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:46 · PVG 17:46 · LAX 02:46 · JFK 05:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.