简体   繁体   English

如何修复文件名或其路径中的语法错误?

[英]how can i fix the syntax error in the file name or its path?

when I run that code it comes out to me this error:当我运行该代码时,它出现了这个错误:

IndexError: string index out of range IndexError:字符串索引超出范围

I have no idea why I comes out to me this error and I don't have any note but if you want to ask me about any thing don't hesitate我不知道为什么我会告诉我这个错误,我没有任何笔记,但如果你想问我任何事情,请不要犹豫

letters_string = "d  g o"
letters_list = []

# Check Space
def convert_letters_to_list(word):
    """
    This Function takes the letters from the users
    and checks if users put more than one space 
    between letters if True It Removes The extra Spaces
    """

    if word[0].isspace() == True:  
        convert_letters_to_list(word[1])
  
    elif word[0].isalpha() == True:    
        letters_list.append((word[0])) + convert_letters_to_list(word[1:])     

    else:    
        convert_letters_to_list(word[1:])


convert_letters_to_list(letters_string)
print(letters_list)

You function is recursive so after a while, any letter index past 0 will be out of the original string.您的函数是递归的,所以一段时间后,任何超过 0 的字母索引都将超出原始字符串。

Here is a fixed version of your code.这是您的代码的固定版本。

letters_string = "d  g o"
letters_list = []

# Check Space
def convert_letters_to_list(word):
    """
    This Function takes the letters from the users
    and checks if users put more than one space 
    between letters if True It Removes The extra Spaces
    """

    if word[0].isalpha() == True or (word[0].isspace() and  not  letters_list[-1].isspace()):    
        letters_list.append((word[0]))
    
    if len(word) == 1:
        return

    convert_letters_to_list(word[1:])


convert_letters_to_list(letters_string)
print(letters_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM