我正在尝试迭代一段代码,以便基本上用模式创建find()函数。我希望检查string索引+1是否返回一个IndexError (如果超出范围,则返回斜杠),以便知道是继续迭代字符串还是返回第一次出现的索引。我尝试了几种不同的方法,因为它在我创建的类中,所以我一直得到一个属性错误。以下是我尝试过的几件事:
with self.assertRaises(IndexError):
p[ind + 1]
print(ind - len(p))
if not p[ind + 1]:
当我试图执行以下代码时,我一直收到"IndexError: string超出范围“错误消息:
#function countLetters(word,letter) should count the number of times
#a particular letter appears in a word.
def countLetters(word, letter):
count=0
wordlen=len(word)
num=0
wordletter=""
while(num<=wordlen):
IndexError:超出范围的列表分配索引
我不能fix.list赋值索引超出范围。你能帮我吗?
z = [0.5, 0.4]
P = 500 #pisa
T = 400 # Ranking
Tc = [343.01, 549.58]
Pc = [667.03, 706.62]
w = [0.011, 0.099]
kij = [[0, -0.0026], [-0.0026, 0]]
Tr = []
Pr = []
k = []
for i in range(0, 1):
Tr[i] = T / Tc[i]
Pr[i] = P / Pc
k[i] = math.e
s='saamaarrr'
for i in range(0, len(s)-1):
while(s[i]==s[i+1]):
i+=1
print(s[i])
IndexError:超出范围的字符串索引
我对蟒蛇很陌生。我知道这个错误的原因。但我不知道为什么会发生在这里。我也读过关于这个问题的其他答案
虽然此代码将引发indexError:
In [1]: lst = [1, 2, 3]
In [2]: lst[3]
IndexError: list index out of range
用“超出范围的索引”对列表进行切片不会产生任何错误。
In [3]: lst[3:]
Out[3]: []
这种设计的基本原理是什么?
def add(nums):
sum = 0
pointer = 0
for item in nums:
if item[pointer] > item[pointer+1]:
sum += int(item)
pointer += 1
else:
pointer += 1
return sum
list = ['5', '7', '6', '4', '3',
我正在尝试创建一个“简单”的脚本。结果并不像我想的那么容易。我知道这个错误:
IndexError: list index out of range
这是整个密码。它发生在if name[2]: --当你输入两个单词时就会发生,但是当输入3个单词时,它就完全工作了。
name = input('Enter name: ').split()
print(name)
print('Voornaam: ' + name[0])
print('Achernaam: ' + name[len(name) - 1])
if name[2]:
prin
当我输入这个:
def FirstReverse(str):
# code goes here
x = len(str)
s = list(str)
while x >= 0:
print s[x]
x = x - 1
# keep this function call here
# to see how to enter arguments in Python scroll down
print FirstReverse(raw_input())
我得到了这个错误
ERROR ::--Traceback (most recent call last):
File
如果执行此代码,将出现以下错误消息:
IndexError:超出范围的列表索引
def reverse_invert(lst):
inverse_list = []
for i in lst:
if isinstance( i, int ):
inverse_list.append(lst[i])
#print(inverse_list)
print(i)
else:
break
return inverse_
为什么这样做:
s = 'xyz'
i = 0
while i < len(s) and s[i] not in 'aeiou':
print(s[i])
i += 1
X y z
..。但这不是?
s = 'xyz'
i = 0
while s[i] not in 'aeiou' and i < len(s):
print(s[i])
i += 1
x跟踪(最近一次调用):文件"<pyshell#135>",第1行,而s
很抱歉问了个很傻的问题,我根本就不是专业人士。我很好奇它是如何发生的,在Python3.5中的代码中,我收到了IndexError,
with open('graph.txt') as f:
text = f.read()
for i in range(len(Text)):
if text[i+1] == '>':
*do something*
第5行: if texti+1 ==‘>’>‘
IndexError:超出范围的字符串索引
但是,当我strip文件读取
text = f.read().st
我对python很陌生,并且使用python3.7版本。我试图在列表中添加一些单词,并将它们转换成数字形式,但是,我得到了错误。
以下是代码:
for file in files:
f = open(direct+file, encoding='utf8')
lines = f.read().split("\n")
for line in lines:
data = []
words = line.split(' ')
fo
我制作了一个程序来自动应用我开发的加密程序。在一段特定的代码中,我继续得到一个"String超出范围“的错误。
这段代码是做什么的?
下面的代码创建一个数组,d。它将字符串的某些部分附加到数组中。代码的目的是从宽度为5的文本中生成一个矩形,这意味着如果原始文本是"abcdefghijkl",那么这个想法就是将其转化为
a b c d e
f g h i j
k l X X X
代码的最后一行给出了错误“字符串索引超出范围”为什么?在前面的代码中,正如我忘记提到的那样,我将X添加到字符串的末尾,使它均匀地划分为5。
d = []
for x in ran
我像这样解析一个.txt:
def parse_file(src):
for line in src.readlines():
if re.search('SecId', line):
continue
else:
cols = line.split(',')
Time = cols[4]
output_file.write('{}\n'.format(
Tim
我正在尝试编写函数map()获取错误IndexError:列表索引超出范围
def map1(fn, a):
i = 0
b = []
while i != len(a):
print(len(a))
i += 1
b.append(fn(a[i]))
return b
具有工作功能
def translate(x):
dicti = {"merry": "god", "c
我试着制作一个小程序来生成这样的文本:
M E T A
E E
T T
A A
我的代码:
line = 0
word = raw_input("")
for i in word:
print i,
print
for i in range(len(word)):
line += 1
print word[line] + " " * (line*2-1) + word[line]
它似乎在技术上起作用,但结果是
回溯(最近一次调用):IndexError: string索引超出范围的文件"python"
我想计算'bob‘在字符串s中出现的次数。我写的代码是:
s = 'qwbobthghdeerxybobhjkhgkjgbob'
num = 0
count = 0
for char in s:
if char == 'b':
letter = s[num+1]
if letter == 'o':
letter = s[num+2]
if letter == 'b':
count =
我有一个问题,在我的代码中
for i in xrange(len(acid1)):
j=0
k=0
while (j<worksheet.ncols):
if (acid1[i].lower()==worksheet.row_values(0)[j+1].lower()):
col1=j+1
while (k<worksheet.nrows):
if (acid2[i].lower()==worksheet
我试图通过传递文件并将其存储在数组中,从命令行获取输入。我知道我的输入是用空格分隔的两个单词,所以我试了一下。
import sys
for line in sys.stdin:
a=[]
a=line.split() #to split the 2 words separated by white space into 2 list elements
print a # just to debug my code
edges(a[0], a[1]) #calling the function edges
对此,我收到了一个错误:-
edges(a[0],
import random
fruits = ['Apple','Mango','Banana','Grapes','Guava']
random.seed(500)
low =0
high = len(fruits)
random_fruits = [fruits[random.randint(low,high)] for i in range(1000)]
这是我的代码,下面是我得到的错误?
IndexError:列出超出范围的索引
我知道这是很奇怪的代码,但尽量不要注意。我只想用这样一种奇怪的方法来解决这个问题。但在这个过程中,我面临着这个问题。你能帮我修一下吗?
in <module>
in reverse_alternate
IndexError: string index out of range
我想它是与模相关的。对吗?
def reverse_alternate(string):
a = string.split(' ')
new = ''
for i, c in enumerate(a):
if i % 2 != 0
使用枚举的For循环不会在循环中删除一个元素时抛出索引超出范围的错误?
L = [1, 4, 8, 5]
try:
for i,item in enumerate(L):
print("Value of {} is {}".format(i, item))
del L[i]
except IndexError as e:
print("Index error: {err}.".format(err=e))
输出:
Value of 0 is 1
Value of 1 is 8
虽然这段代码会导致错误
L = [1, 4, 8, 5]
import shutil
import os
import random
source = os.listdir("/1/scratch/bacteria")
destination = "/1/scratch/library/Bacteria/"
for dir in source:
result = []
for i in range(100):
index = random.randrange(0, len(source))
result.append(dir[ind
我正在尝试解决Project Euler中的问题18。我的代码是:
lines = []
lines.append([3])
lines.append([7, 4])
lines.append([2, 4, 6])
lines.append([8, 5, 9, 3])
i = len(lines) - 1
while i != -1:
for j in range(0, len(lines[i - 1])):
a = lines[i][j]
b = lines[i][j + 1]
if a > b:
lin
我的python代码中有'em dash‘字符,在特定的txt文件中用它分割一行。
with open(path, 'r') as r:
number = r.readline()
num = number.split(' — ')[1].replace('\n',' — ')
它在ubuntu和python3.4下工作得很好,但是当在windows 7 (python3.4)下运行代码时,会得到以下错误。
num = number.split(‘\u2014 ') 1.替换(’\n‘,’\u20
我有一个函数将项附加到列表中,然后使用.format()打印每个
如果我尝试打印self.checked[0],它将打印选定的项,但如果它尝试self.checked[0],self.checked[1]系统崩溃。
问题是,当我试图打印每个项目时,系统会显示以下错误:
self.display_event_mouhafaza.setText(" {} over {}".format(self.checked[0],self.checked[1]))
IndexError: list index out of range
代码中的错误在哪里?
def selectionChan