[英]String index out of range in Python
def romanToNum(word):
word = word.upper()
numeralList2 = list(zip(
[1000, 500, 100, 50, 10, 5, 1],
['M', 'D', 'C', 'L', 'X', 'V', 'I']
))
num = 0
x = []
a = 0
b = 2
if len(word) % 2 != 0:
word = word + "s"
for i in range(0,len(word)):
x.append(word[a:b])
a = a + 2
b = b + 2
print(x[i])
for n in x:
for nNum,rNum in numeralList2:
if n == rNum:
num = nNum + num
elif n == (n[0] + n[1]):
num = (nNum*2) + num
elif n[0] == rNum:
r1 = 0
r1 = nNum
elif n[1] == rNum:
r2 = 0
r2 = nNum
elif r1 < r2:
num = num + (r2 - r1)
elif r1 > r2:
num = num + (r1 + r2)
return num
romanToNum("xxx")
I am getting the following error:我收到以下错误:
elif n == (n[0] + n[1]):
IndexError: string index out of range
and it doesn't matter where I put that in the loop, it just wont recognize that n
has an index value.我把它放在循环中的哪个位置并不重要,它只是不会识别
n
有一个索引值。
I also get this error: Traceback (most recent call last):
我也收到此错误:
Traceback (most recent call last):
which points to when i call my function: romanToNum("xxx")
当我调用我的函数时指向它:
romanToNum("xxx")
I'm not really sure what's going on because I added a print
statement to where I'm appending my list and there is an index of at least [0]
when I print it all out.我真的不知道是怎么回事,因为我加入了
print
语句,在那里我追加我的列表,并有至少一个指数[0]
当我打印了这一切。 Any help here?这里有什么帮助吗?
I have looked through stack for similar questions but the solution for them is an indentation or because they had a negative index( [-1]
) or something along those lines but all my indentation is correct and my index's are all positive.我已经通过堆栈查看了类似的问题,但它们的解决方案是缩进,或者因为它们有一个负索引(
[-1]
)或类似的东西,但我所有的缩进都是正确的,我的索引都是正的。
Well n
is an element of x
.那么
n
是x
一个元素。 The IndexError
on the line n == n[0] + n[1]
means that a certain n
has length less than 2
.行
n == n[0] + n[1]
上的IndexError
意味着某个n
长度小于2
。
You added an word = word + 's'
to probably guard against having one character elements in x
but it doesn't really work.您添加了一个
word = word + 's'
以防止在x
中包含一个字符元素,但它并没有真正起作用。
If you look at how you build the x
list you do:如果您查看如何构建
x
列表,请执行以下操作:
x = []
a = 0
b = 2
if len(word) % 2 != 0:
word = word + "s"
for i in range(0,len(word)):
x.append(word[a:b])
a = a + 2
b = b + 2
print(x[i])
So in your example you start with x = []
and word = 'XXX'
.因此,在您的示例中,您以
x = []
和word = 'XXX'
。 Then you add an s
to obtain word = 'XXXs'
.然后添加
s
以获得word = 'XXXs'
。
The loop over i
does the following: i
上的循环执行以下操作:
i=0
so x.append(word[0:2]); a = a+2; b = b+2
i=0
所以x.append(word[0:2]); a = a+2; b = b+2
x.append(word[0:2]); a = a+2; b = b+2
x.append(word[0:2]); a = a+2; b = b+2
so that x = ['XX']
and a=2
and b=4
. x.append(word[0:2]); a = a+2; b = b+2
使得x = ['XX']
和a=2
和b=4
。
i=1
so x.append(word[2:4]); a = a+2; b = b+2
i=1
所以x.append(word[2:4]); a = a+2; b = b+2
x.append(word[2:4]); a = a+2; b = b+2
x.append(word[2:4]); a = a+2; b = b+2
so that x = ['XX', 'Xs']
and a=4
and b=6
. x.append(word[2:4]); a = a+2; b = b+2
使得x = ['XX', 'Xs']
和a=4
和b=6
。
i=2
so x.append(word[4:6]); a = a+2; b = b+2
i=2
所以x.append(word[4:6]); a = a+2; b = b+2
x.append(word[4:6]); a = a+2; b = b+2
x.append(word[4:6]); a = a+2; b = b+2
so that x = ['XX', 'Xs', '']
and a=6
and b=8
. x.append(word[4:6]); a = a+2; b = b+2
使得x = ['XX', 'Xs', '']
和a=6
和b=8
。i=3
so x.append(word[6:8]); a = a+2; b = b+2
i=3
所以x.append(word[6:8]); a = a+2; b = b+2
x.append(word[6:8]); a = a+2; b = b+2
x.append(word[6:8]); a = a+2; b = b+2
so that x = ['XX', 'Xs', '', '']
and a=8
and b=10
. x.append(word[6:8]); a = a+2; b = b+2
使得x = ['XX', 'Xs', '', '']
和a=8
和b=10
。 And here you see that n
can be the empty string, which means when doing n == n[0] + n[1]
you end up with an IndexError
.在这里你看到
n
可以是空字符串,这意味着当你做n == n[0] + n[1]
你最终会得到一个IndexError
。
I believe you wanted to group the characters two by two, but then the i
should use a step of 2
:我相信您想将字符两两分组,但是
i
应该使用2
的步骤:
for i in range(0, len(word), 2):
x.append(word[i:i+2])
In this way i
is 0
, then 2
, then 4
etc这样
i
是0
,然后是2
,然后是4
等等
By the way: once you have fixed this the condition n == n[0] + n[1]
seems pretty odd, because if n
is a two character string (as it should be if you fix the code) then the condition will always be true.顺便说一句:一旦你解决了这个问题,条件
n == n[0] + n[1]
看起来很奇怪,因为如果n
是一个两个字符的字符串(如果你修复代码应该是这样)那么条件就会永远是真实的。 What are you trying to really do here?你到底想在这里做什么?
This is the culprit:这是罪魁祸首:
for i in range(0,len(word)):
x.append(word[a:b])
a = a + 2
b = b + 2
At the end of this loop, x will be ['XX', 'Xs', '', '']
.在此循环结束时, x 将是
['XX', 'Xs', '', '']
。 Since you are grouping the characters in groups of two, the total number of groups will be half the length of the string.由于您将字符分组为两个一组,因此组的总数将是字符串长度的一半。 So just halve the number of iterations with
range(0,len(word)/2)
or range(0,len(word),2)
所以只需将
range(0,len(word)/2)
或range(0,len(word),2)
的迭代次数减半
You have a problem with your first for loop that goes farther than expected, affecting an empty string to x[i]
.您的第一个 for 循环出现问题,超出预期,将空字符串影响到
x[i]
。 It should probably be : for i in range(int(len(word)/2)):
它可能应该是:
for i in range(int(len(word)/2)):
Then your second loop needs fixing too.然后你的第二个循环也需要修复。
if n == rNum :
is never realised since rNum is a one character string and x's length is 2. Try n == rNum+"s"
. if n == rNum :
永远不会实现,因为 rNum 是一个单字符串并且 x 的长度是 2。尝试n == rNum+"s"
。 n == n[0] + n[1]
is always True for a string of 2 characters.对于 2 个字符的字符串,
n == n[0] + n[1]
始终为 True。 You must mean n == rNum * 2
你的意思是
n == rNum * 2
Also, the use of x += 1
is recommended instead of x = x + 1
.此外,建议使用
x += 1
而不是x = x + 1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.