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

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

  •  
  •   Pzdmultiverse · 2016-07-19 18:19:57 +08:00 · 2093 次点击
    这是一个创建于 2831 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2016-07-20 15:24:51 +08:00
    yangtukun1412
        1
    yangtukun1412  
       2016-07-19 18:23:05 +08:00
    递归调用或者死循环去掉一个
    Yinz
        2
    Yinz  
       2016-07-19 18:25:10 +08:00
    倒数第二行又调用了一次函数,递归调用了,你这样每多输入一次非'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
       2016-07-19 18:54:21 +08:00 via Android
    @Yinz
    @yangtukun1412 懂了,非常感谢
    jixiangqd
        4
    jixiangqd  
       2016-07-20 12:50:28 +08:00
    好像除了代码是 python 写的跟 python 并没啥关系。。。
    Pzdmultiverse
        5
    Pzdmultiverse  
    OP
       2016-07-20 13:04:32 +08:00 via Android
    @jixiangqd 是这样,标题该写成[新手编程疑问贴]…😂
    jixiangqd
        6
    jixiangqd  
       2016-07-20 15:20:55 +08:00
    @Pzdmultiverse 加油~
    Pzdmultiverse
        7
    Pzdmultiverse  
    OP
       2016-07-20 15:24:51 +08:00
    @jixiangqd 共勉~-.-
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2498 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 15:39 · PVG 23:39 · LAX 08:39 · JFK 11:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.