是不是一碗粉 2019-08-16 08:28 采纳率: 100%
浏览 343
已采纳

循环为什么没有退出?

新手python学徒,求教。以下为代码

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
active = True 
while active: 
    message = input(prompt)
    if message != 'quit':
        print(message)
    else:
        active = False

以下为结果

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit
quit

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit

进程已结束,退出代码0

问题一:
为什么第一遍输入quit后循环并没有停止?
问题二:
不符合条件语句if if message != 'quit': 的情况执行了print(message)的原因是什么?
问题三:
为什么第二遍输入quit后循环停止了?
解释器为Pycharm

  • 写回答

3条回答 默认 最新

  • JonathanYan 2019-08-16 11:40
    关注

    稍微修改了程序,复现了你的结果,看下就懂了,以后debug可以用这种方法
    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?