python - Python打破了一个while循环
问题描述
我对 Python 和这个网站有点陌生。我需要有关我的代码的任何帮助。当用户输入一个值但我遇到问题时,我试图打破循环。我正在为我的学校做一个聊天机器人项目。
while ans:
user_input = input("How are you?: (or press enter to quit) ")
user_input = ''.join(ch for ch in user_input if ch not in exclude)
user_words = user_input.split()
解决方案
如果您正在寻找任何输入,那么您将不需要 while 循环,Python 将在等待用户输入时暂停程序。
user_input = input("How are you?: (or press enter to quit) ")
user_input = ''.join(ch for ch in user_input if ch not in exclude)
user_words = user_input.split()
或者,如果您想等待特定值,则需要设置一个条件来中断循环。
ans = True
while ans != "Quit":
user_input = input("How are you?: (or press enter to quit) ")
user_input = ''.join(ch for ch in user_input if ch not in exclude)
user_words = user_input.split()
if user_input == "Quit":
ans = "Quit"
或者
while ans:
user_input = input("How are you?: (or press enter to quit) ")
user_input = ''.join(ch for ch in user_input if ch not in exclude)
user_words = user_input.split()
if user_input == "Quit":
break
推荐阅读
- python - NVIDIA Jetson Nano 开发者套件上的 PWM
- php - WooCommerce:在结帐时更改帐单字段的 HTML 结构
- shex - 当部分 IRI 在三重存储中时,使用 Shex 验证 RDF 的正确方法是什么?
- deployment - 删除一个 pod 时如何延迟部署中的新 pod 创建?
- vba - 当活动单元格位于特定列上时运行宏
- django - 如何在 MongoDB 中使用 Django 3?
- php - json_encode 响应的警报成功引导程序
- windbg - 如何解决 WinDbg 中的错误“无法在单处理器内核分类转储上切换处理器”?
- javascript - JavaScript API 的 HERE 地图:计算从多边形到标记的距离
- c - (2D int array pointer to pointer) 为什么会出现分段错误?