python - 无法理解为什么并且无法正常工作
问题描述
我试图理解为什么Python没有进入循环并以错误代码退出。
同时OR
条件很好。
def user_choice():
choice=''
within_range = False
while choice.isdigit == False and within_range == False:
choice=input('Enter valid selection (1-9): ')
if choice.isdigit() == False:
print('You entered non-digit value, please input digit')
if choice.isdigit() == True:
if int(choice) in range(0,10):
within_range=True
else:
within_range=False
return int(choice)
解决方案
以下代码块中还有另一个缺陷:(您应该将“and”更改为“or”以获得正确的结果,否则无论您输入什么整数(不在范围内),它都会返回!
def player_choice():
choice=' '
within_range = False
while choice.isdigit() == False **or** within_range == False:
choice=input('Enter valid selection (0, 1, 2): ')
if choice.isdigit() == False:
print('You entered non-digit value, please input digit(0, 1, 2)')
if choice.isdigit() == True:
if int(choice) in range(0,3):
within_range=True
else:
within_range=False
return int(choice)
推荐阅读
- amazon-web-services - 如何对使用层方法的 lambda 逻辑进行单元测试?
- powerbi - DAX 总计未在 Power BI 中显示正确的值
- python - 3D点云可视化不同
- c# - 带有选择列表的下拉列表
- https - 为 Asp.net core web api 启用 HTTPS 安全连接
- e4 - 构建 RCP 应用程序没有运行生命周期管理器
- python - Python:在不同长度的视频中提取“n”帧
- selenium - Selenium - IE 驱动程序:单击 js 中定义的链接,_blank 什么都不做
- excel - Excel:在不同列上排序时如何将单元格保持在一起
- javascript - 受控/非受控输入