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
theohateonion
V2EX  ›  Python

新手 pythoer 求大家看一个小程序

  •  
  •   theohateonion · 2015-10-10 15:24:21 +08:00 · 2772 次点击
    这是一个创建于 3093 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def is_prime(x):
    if x <= 1:
            return False
        if x == 2:
            return True
        for i in range(2,x):
            if x % i == 0:
                return False            
            else:
                return True
    

    自己写的一个判断素数的小程序,但是不知道为什么会把 9 , 15 等这些数判为素数。。

    15 条回复    2015-10-12 07:38:34 +08:00
    zzy8200
        1
    zzy8200  
       2015-10-10 15:28:53 +08:00 via iPhone
    去掉 else
    saber000
        2
    saber000  
       2015-10-10 15:32:47 +08:00
    姑且认为第一行的缩进被吞了,else 和下面的代码往左一个缩进
    BraveRBT
        3
    BraveRBT  
       2015-10-10 15:34:33 +08:00
    注意缩进对其
    另外应该是 Pythoner~?
    zzy8200
        4
    zzy8200  
       2015-10-10 15:38:24 +08:00 via iPhone
    去掉 else:后把 return True 拿到最外面,你这个只会判断是否是偶数
    BraveRBT
        5
    BraveRBT  
       2015-10-10 16:34:48 +08:00
    直接给楼主附上修改后的代码咯 :)
    def is_prime(x):

    if x <= 1:
    return False
    if x == 2:
    return True
    for i in range(2,x):
    if x % i == 0:
    return False
    return True
    BraveRBT
        6
    BraveRBT  
       2015-10-10 16:35:55 +08:00
    不好意思
    <pre>
    def test_prime(x):
    if x <= 1:
    return False
    if x == 2:
    return True
    for i in range(2,x):
    if x % i == 0:
    return False
    return True
    </pre>
    BraveRBT
        7
    BraveRBT  
       2015-10-10 16:37:49 +08:00
    没搞懂 V2EX 的回复格式,哪位指导一下
    Thanks
    imlonghao
        8
    imlonghao  
       2015-10-10 16:48:51 +08:00   ❤️ 1
    @BraveRBT 回帖不支持 Markdown ,使用 gist 可破
    twor2
        9
    twor2  
       2015-10-10 16:49:36 +08:00   ❤️ 1


    PS: @BraveRBT 回复不支持 md ,只有发帖支持
    twor2
        10
    twor2  
       2015-10-10 16:50:50 +08:00
    billgreen1
        11
    billgreen1  
       2015-10-10 16:59:37 +08:00 via iPhone
    看看各种筛法吧
    Arnie97
        12
    Arnie97  
       2015-10-10 17:09:09 +08:00
    新手大概不知道 list comprehension ?

    (x > 1) and all(x % i for i in range(2, x))
    twor2
        13
    twor2  
       2015-10-10 17:21:01 +08:00
    @Arnie97 新手,知道,但用得少,没意识,没有形成条件反射
    BraveRBT
        14
    BraveRBT  
       2015-10-11 21:21:52 +08:00
    @imlonghao
    @twor2
    了解了 Thanks~!
    theohateonion
        15
    theohateonion  
    OP
       2015-10-12 07:38:34 +08:00
    谢谢各位啦。我找到问题了~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5432 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 08:44 · PVG 16:44 · LAX 01:44 · JFK 04:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.