python - 如何在两个数字之间进行输入
问题描述
我想强制用户输入在两个数字之间,例如 5-15,如果它不在或不等于这些数字,请输入这两个数字之间的数字并请求另一个输入。
我已经有一个强制你输入整数的输入。
while True:
try:
# asking for race length
race_length = int(input("Choose the Length You Would Like You Race To Be (Between 5 and 15)"))
except ValueError:
print("Sorry, I didn't understand that.")
#if an interger isn't entered do loop above to avoid and error
continue
else:
#race length succesfully found
#finished the loop
break
解决方案
Use if-else to check if the value is within the required range or not if yes then assign it to the race_length
if not ask user to enter again.
if(x>5 and x<15):
race_length = x
else:
input('Choose the Length You Would Like You Race To Be (Between 5 and 15)')
推荐阅读
- java - Spring Boot 未从 IntelliJ 部署 WAR
- sql - 在所有记录中触发将值增加 1 而不是唯一更新的记录
- javascript - 通过鼠标单击显示/隐藏 div 搜索下拉菜单
- mysql - 如何在同一个查询中返回不同的结果?
- php - 当变量在文件名中时,PHP file_exists 返回 false
- python - 如何将文件读入列表
- python - 使用正则表达式的文件模式匹配变量
- javascript - 使用 Import * from single export
- macos - Ubuntu 16.04 映像的 Docker 构建失败 - 连接失败,返回非零代码:100
- windows - 为不受支持的设备安装虚拟(空)驱动程序