python-3.x - 索引在输入范围内
问题描述
我已经构建了一个应用程序,用于使用 CLI 对 S3 存储桶执行操作。我正在尝试验证用户的输入。
我有一个包含所有项目的数组,这些项目基本上是从 S3 本身获取的。我验证它不是 0,然后用户需要在该数组长度的范围内进行选择,否则会提示重新输入选择。很简单,但我无法让它工作。如果可能,请提供帮助。那是我需要的一段代码,已经为此奋斗了一段时间。
if objects is not None:
obj_count=0
# List the object names
for obj in objects:
print(f'{obj_count}. {obj["Key"]}')
obj_count = obj_count + 1
if obj_count > 0:
# ERROR CHECKING: Need to loop here until a
# valid int is entered that is in
# the range of available bucket objects
object_to_delete_index = input('Which object to delete?:')
if (object_to_delete_index in range(len(objects), len(objects)-1)):
print(f'Deleting object {objects[int(object_to_delete_index)]["Key"]}')
# # Delete the object
if delete_object(selected_bucket_name, objects[int(object_to_delete_index)]["Key"]):
logging.info(f'{selected_bucket_name} was deleted from {obj["Key"]}')
else:
print('Bucket is empty')
else:
"Input correct selection"
解决方案
推荐阅读
- reactjs - 无法从多个子组件(同一组件)向父组件发送不同的回调参数
- sql - 通过与其他表合并计算第一次登录和最后一次注销之间的差异 - [postgresql]
- laravel - 补丁方法不允许!有替代的请求方法吗?
- objective-c - 如何使 Objective-C 仅针对构建模块?
- php - PHP Intelephense - 方法与子方法不兼容
- mysql - 在 2 个不同字段中具有相同值的不同值的总和以及在 2 个不同字段中具有不同值的所有值的总和
- web - Odoo - 根据网站字段隐藏网站菜单
- spring-boot - 在模型内部构造一个数组列表
- python - Django - Python 从字典中获取列表返回一个数字而不是列表
- python - 当每个请求都需要时,在 Django 请求中传递参数的推荐方法是什么?