Python 如何修复此代码中的此错误?索引超出范围

Python 如何修复此代码中的此错误?索引超出范围,python,Python,我有一个函数,它允许我放入任何字符串并按字母顺序返回高值。例如,如果我有一个像“Hello world”这样的句子,我们假设world比Hello大,那么我们将返回world 这个高值是由我在string\u index\u in\u char中设置的索引测量的 def func(string): char = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r'

我有一个函数,它允许我放入任何字符串并按字母顺序返回高值。例如,如果我有一个像“Hello world”这样的句子,我们假设
world
Hello
大,那么我们将返回
world

这个高值是由我在
string\u index\u in\u char
中设置的索引测量的

def func(string):
    char = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    split_string = string.split()
    i = 0
    x = ""

    # get letters
    string = string.lower()
    for s in string:
        if s in char:
            string_index_in_char = char.index(s) + 1
        else:
            string_index_in_char = 0
        x += str(string_index_in_char)
        split_any_char_not_letters = x.split("0")
    # convert string into interger in list  
    for i in range(0, len(split_any_char_not_letters)): 
        split_any_char_not_letters[i] = int(split_any_char_not_letters[i])      
    convert_to_int = split_any_char_not_letters 

    # (split_string, convert_to_int) comparison
    n = convert_to_int.index(max(convert_to_int))
    my_string = split_string[n]
    print("%s -> %s" %(string, my_string.lower()))


#func("This Is my home")
func("Hello wOrld")
我得到这个错误:

Traceback (most recent call last):
  File "D:\courses\udacity-python\python\Exam\alphabet\alphabet 5.py", line 27, in <module>
    func("This Is my home")
  File "D:\courses\udacity-python\python\Exam\alphabet\alphabet 5.py", line 23, in func
    my_string = split_string[n]
IndexError: list index out of range
回溯(最近一次呼叫最后一次):
文件“D:\courses\udacity python\python\Exam\alphabet\alphabet 5.py”,第27行,在
func(“这是我的家”)
文件“D:\courses\udacity python\python\Exam\alphabet\alphabet 5.py”,第23行,func
我的字符串=拆分字符串[n]
索引器:列表索引超出范围

只要我键入任何其他单词,如“伙计,我需要一辆出租车到乌布”或一些单词,我就不会出现任何问题。那么,为什么在我键入“hello world”时,它没有向我发送此错误,却收到了此错误呢?

按照@Parakiwi:
的建议进行了更新 在您的示例中,当您拆分
0
时会出现问题:这是我家,t位于位置
20
,因为您按
0
拆分,它会通过向拆分列表添加额外值来影响代码,从而导致错误。更改为除char index以外的任意数字(如
)将起作用:

更改:

if s in char:
    string_index_in_char = char.index(s) + 1
    print(string_index_in_char)
else:
    string_index_in_char = "^"
x += str(string_index_in_char)
split_any_char_not_letters = x.split("^")
之前:

if s in char:
            string_index_in_char = char.index(s) + 1
        else:
            string_index_in_char = 0
        x += str(string_index_in_char)
        split_any_char_not_letters = x.split("0")  

但是,您需要处理两个单词相等的情况。

您可以添加完整的错误跟踪吗?我修改了我的帖子,您将看到下面的代码跟踪,但是如果我在代码中输入除“hello world”以外的任何单词,如“this is my home”,我将得到此错误。请小心选择任意数字。代码段“can i eat”的计算结果为“3113999995120”,这可能会在以后按字符串“999”拆分时引起问题。此外,如果文本中的字母“i”中有三次“for”,则可能出现“999”,例如罗马数字值为3,因此“苏格兰的詹姆斯三世”会给您带来问题。@Parakiwi,thank将更新答案,删除数字-9990您可以简单地使用逗号作为分隔符,添加一个“,”并稍后再次用它拆分?这将避免对数字和数字的混淆letters@Parakiwi我想在一个句子中也可能有逗号,所以我会添加一个特殊字符
^
作为更新的answer@abdelhamed您使用0进行拆分导致了问题,因此请使用特殊字符,而不是按拆分条件中的数字进行拆分。我已经更新了答案。