Python-IndexError: list index out of range
Error:IndexError: list index out of range
Where?
对Python中有序序列进行按索引取值的时候,出现这个异常
Why?
对于有序序列: 字符串 str 、列表 list 、元组 tuple进行按索引取值的时候,默认范围为 0 ~ len(有序序列)-1,计数从0开始,而不是从1开始,最后一位索引则为总长度减去1。当然也可以使用 负数表示从倒数第几个,计数从-1开始,则对于有序序列,总体范围为 -len(有序序列) ~ len(有序序列)-1,如果输入的取值结果不在这个范围内,则报这个错
Way?
检查索引是否在 -len(有序序列) ~ len(有序序列)-1 范围内,修改正确
错误代码:
name = "beimenchuixue"
students = ["beimenchuixue", "boKeYuan", "Python", "Golang"]
print(name[20])
print(students[4])
正确代码:
name = "beimenchuixue"
students = ["beimenchuixue", "boKeYuan", "Python", "Golang"]
print(name[3])
print(students[3])
Python-IndexError: list index out of range的更多相关文章
- Python常见错误:IndexError: list index out of range
用python写脚本查询字典时,在遍历字典时循环到某一项时老是报错 出现这种错误有两种情况: 第1种可能情况 list[index]index超出范围 第2种可能情况 list是空值就会出现 In ...
- python报错IndexError: list index out of range
今天写个ping vpn的python脚本,报错IndexError: list index out of range 最后查看是python读取文件中出现空格 去掉空格即可
- pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”
pyinstaller将py打包为exe文件,用pysintaller居然报错 File "c:\anaconda3\lib\site-packages\PyInstaller\depend ...
- IndexError:string index out of range
IndexError:string index out of range 出现在下标越界的情况,如 item[1],可能为空的时候下标就会越界
- 在数组添加元素时报错:IndexError: list index out of range
今天第一次发随笔还有许多不足之处,欢迎评论!!! 最近在写一个成语接龙的小游戏,结果在数组添加元素时报错:IndexError: list index out of range 源码: import ...
- mac安装MySQLdb:IndexError: string index out of range
使用mac安装MySQLdb的时候出现string index out of range 大概的错误是这样的: 然后尝试手动安装,我下载了包后,依然出现这个错误. 于是百度了下: https://ww ...
- IndexError: list index out of range Python常见错误
引用超过list最大索引,此错误非常常见,注意列表的元素个数 ----------------------------------------------
- MacOS 安装MysqlDB 问题解决方案( 解决 IndexError: string index out of range)
pip install MySQL-python时报错如下: Command "python setup.py egg_info" failed with error code 1 ...
- IndexError: list index out of range的错误原因
第1种可能情况list[index]index超出范围 第2种可能情况list是一个空的 没有一个元素进行list[0]就会出现该错误 ————————————————版权声明:本文为CSDN博主「m ...
- Mac安装Mysql-python遇到的坑,被这俩报错反复摩擦:'my_config.h' file not found 和 IndexError: string index out of range
最后Stackoverflow上面的大神解决了问题: Link brew install mysql brew unlink mysql brew install mysql-connector-c ...
随机推荐
- Win 10 蓝屏,出现DRIVER_POWER_STATE_FAILURE的解决方法
笔者个人笔记本电脑,用的是华硕的飞行堡垒FZ系列,上个月装了个Ubuntu的系统,之后换回Windows后,电脑疯狂蓝屏,错误代码只有这个DRIVER_POWER_STATE_FAILURE.一开始我 ...
- Java面试题(多线程篇)
多线程 35.并行和并发有什么区别? 1.并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生. 2.并行是在不同实体上的多个事件,并发是在同一实体上的多个事件. 3.在 ...
- seo工程师是什么,需要什么技能?
http://www.wocaoseo.com/thread-222-1-1.html seo工程师是什么,SEO工程师是目前需求较大的一种职业,是搜索引擎营销的一种,主要是是通过网站优化技 ...
- flutter 设置状态栏的背景与颜色
flutter 设置状态栏的背景与颜色 导包 import 'dart:io'; import 'package:flutter/services.dart'; 在main()函数中添加以下函数, v ...
- C# 解析获取Url参数值
今天遇到一个需求,需要处理通过接口传过来的一个参数,参数内容为一个拼接好的Url地址,且该地址还会携带了一些额外的参数,包括但不限于数字,字符串,json串.样例如下: http://www.cple ...
- 【Android】Android开发实现带有反弹效果,仿IOS反弹scrollview详解教程
作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 首先给大家看一下我们今天这个最终实现 ...
- 集成react-native-image-picker时,报错Couldn't get file path for photo
1. 版本环境: "react": "16.13.1", "react-native": "0.63.2", " ...
- Python自动化测试入门科技树
Python基础: 入门语法和数据类型: 编码环境安装基本语法 常用数据类型 常用运算符 Llist.Tuple.Dict.if&for.while Python进阶: 函数: 语法 内置函数 ...
- Educational Codeforces Round 68 (Rated for Div. 2)-C-From S To T
You are given three strings ss, tt and pp consisting of lowercase Latin letters. You may perform any ...
- [BUUOJ记录] [BJDCTF 2nd]fake google
本题考查python jinjia2的ssti,其实tplmap直接梭哈都可以 随便输入一个值,查看源代码就能看到一个Hint: 用下面的流程图测试一下,看看是什么模板引擎: 用Payload测试之后 ...