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

小白问各位大佬一个问题,为什么我这个程序跳不出主程序,试了几个方法都不可用,求指点

  •  
  •   Supermanhh · 2020-05-27 17:22:13 +08:00 · 2700 次点击
    这是一个创建于 1428 天前的主题,其中的信息可能已经有所发展或是发生改变。
    上面代码省略掉了,直接下面的,就是执行操作后,选择了 ‘n’,程序还是会运行


    # 是否继续
    def cont(a):
    choice = input("是否继续?(y or n)")
    if choice == 'y':
    a = 1
    else:
    a = 0
    return a
    #程序头
    if __name__ == "__main__":
    flag = 1
    while flag == 1:
    print("=================欢迎使用通讯录=============")
    choiceshow = """
    请选择您的进一步选择:
    (添加)往数据库添加数据
    (删除)删除数据库中的数据
    (修改)修改数据库中的数据
    (查询)查询数据库中的数据
    选择你要进行的操作:
    """
    choice = input(choiceshow)
    if choice == "添加":
    addinfo()
    cont(flag)
    elif choice == "删除":
    deldb()
    cont(flag)
    elif choice == "修改":
    alter()
    cont(flag)
    elif choice == "查询":
    searchdb()
    cont(flag)
    else:
    print("输入的操作有误,重新输入!")
    15 条回复    2020-05-28 10:39:04 +08:00
    shelterz
        1
    shelterz  
       2020-05-27 17:28:49 +08:00
    if cont(flag) == 0: break;
    Latin
        2
    Latin  
       2020-05-27 17:45:35 +08:00
    贴代码用 markdown
    ```
    code
    ```
    Supermanhh
        3
    Supermanhh  
    OP
       2020-05-27 17:56:02 +08:00
    @Latin 新人第一次发帖,下次试试
    Supermanhh
        4
    Supermanhh  
    OP
       2020-05-27 17:58:39 +08:00
    @shelterz 这个写在主程序里吗,我试了用 while flag == 0: break 不行
    zdt3476
        5
    zdt3476  
       2020-05-27 18:01:25 +08:00
    flag = cont(flag)
    duyuyouci
        6
    duyuyouci  
       2020-05-27 18:24:32 +08:00
    你的 flag 永远是 1,怎么跳出来
    Supermanhh
        7
    Supermanhh  
    OP
       2020-05-27 19:22:33 +08:00 via iPhone
    @zdt3476 #5 这样写的话直接就报错了,
    shintendo
        8
    shintendo  
       2020-05-27 19:37:21 +08:00
    很简单

    你的问题是什么?是跳不出 while 循环。
    while 循环的条件是什么?是 flag == 1 。
    那怎么才能跳出循环?改变 flag 的值,使它不等于 1 。

    你的代码里,有任何改变 flag 值的语句吗?如果没有,那当然跳不出循环;如果你认为有,那把你认为的语句指出来,我们告诉你为什么它不改变 flag 的值。
    cherbim
        9
    cherbim  
       2020-05-27 22:15:59 +08:00
    你是问为什么 while True 这个循环不会打破,你好像没有改变 flag 的值啊
    还有你这个操作很骚啊,cont(flag),你把 flag 传到函数 cont 里,但是里面直接来一个 a =1 或者 a = 0,这个 flag 完全没用啊
    换成 flag = cont(), 把函数 cont 里的参数去掉,完全没用
    cherbim
        10
    cherbim  
       2020-05-27 22:18:18 +08:00
    def cont():
    choice = input("是否继续?(y or n)")
    if choice == 'y':
    return 1
    else:
    return 0
    然后主函数里 flag = cont(),这样,输入 y flag=1,输入 n flag=0
    mzotw2babm
        11
    mzotw2babm  
       2020-05-28 08:59:15 +08:00
    因为你原程序里 cont(flag)的返回值并不会直接赋给 flag 。返回值 和 赋值,是两个操作。cont(a)中的 a 只是个形参,而不是你程序中声明的变量。当你使用 cont(flag)时,flag 作为形参,作用仅是向 cont()内传递值,并不会改变变量 flag 本身。

    按 @cherbim 的答案修改即可。
    Supermanhh
        12
    Supermanhh  
    OP
       2020-05-28 09:09:38 +08:00
    @cherbim 昨天又研究了一下,可以跳出来了,跟你这个方法也类似,只是要两次取消‘n’才结束程序
    crella
        13
    crella  
       2020-05-28 10:07:48 +08:00 via Android
    这教程写的有问题。把 cont 函数的内容移植到 while true 的循环末尾就好了,略加修改就好了。

    建议放弃看这代码所在的教程、视频、书,因为写得不好。
    Supermanhh
        14
    Supermanhh  
    OP
       2020-05-28 10:38:10 +08:00
    @mzotw2babm 多谢指点,现在明白过来了,cont()只是返回了 1 or 0 并没有对主程序的 flag 实际赋值,现在反应过来了
    Supermanhh
        15
    Supermanhh  
    OP
       2020-05-28 10:39:04 +08:00
    @crella 是的,,我也发现了这书里面代码写的也不规范,放弃了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3465 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:04 · PVG 19:04 · LAX 04:04 · JFK 07:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.