首页 > 解决方案 > 索引在输入范围内

问题描述

我已经构建了一个应用程序,用于使用 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"

标签: python-3.xamazon-s3boto3

解决方案


推荐阅读