推荐学习书目
› 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
Pzdmultiverse
V2EX  ›  Python

[新手 python 疑问帖]这个怎么有问题呢?

  •  
  •   Pzdmultiverse · Jul 19, 2016 · 2792 views
    This topic created in 3722 days ago, the information mentioned may be changed or developed.
    import math
    def eval_loop():
        while True:
            s = input('enter your str:\n')
            if s == 'done':
                print('done')
                break
            result = eval(s)
            print(result)
            eval_loop()
    eval_loop()
    

    运行之后,第一次就输入done,就结束了,如果第一次不是输入done,那么再输入done,程序还会继续运行,这个情况是为啥呢。。

    E:--->python helloworld.py
    enter your str:
    done
    done
    
    E:--->python helloworld.py
    enter your str:
    1+2
    3
    enter your str:
    done
    done
    enter your str:
    
    
    7 replies  •  2016-07-20 15:24:51 +08:00
    yangtukun1412
        1
    yangtukun1412  
       Jul 19, 2016
    递归调用或者死循环去掉一个
    Yinz
        2
    Yinz  
       Jul 19, 2016
    倒数第二行又调用了一次函数,递归调用了,你这样每多输入一次非'done'字符,你就需要多输一次'done'来跳出循环,改成这样就好了
    import math
    def eval_loop():
    while True:
    s = input('enter your str:\n')
    if s == 'done':
    print('done')
    break
    result = eval(s)
    print(result)
    eval_loop()
    Pzdmultiverse
        3
    Pzdmultiverse  
    OP
       Jul 19, 2016 via Android
    @Yinz
    @yangtukun1412 懂了,非常感谢
    jixiangqd
        4
    jixiangqd  
       Jul 20, 2016
    好像除了代码是 python 写的跟 python 并没啥关系。。。
    Pzdmultiverse
        5
    Pzdmultiverse  
    OP
       Jul 20, 2016 via Android
    @jixiangqd 是这样,标题该写成[新手编程疑问贴]…😂
    jixiangqd
        6
    jixiangqd  
       Jul 20, 2016
    @Pzdmultiverse 加油~
    Pzdmultiverse
        7
    Pzdmultiverse  
    OP
       Jul 20, 2016
    @jixiangqd 共勉~-.-
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5080 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 01:29 · PVG 09:29 · LAX 18:29 · JFK 21:29
    ♥ Do have faith in what you're doing.