【问题标题】:string index out of range/ invalide syntex字符串索引超出范围/语法无效
【发布时间】:2020-06-04 01:55:24
【问题描述】:

我在命令行中运行代码时出现“字符串索引超出范围”错误,而它给出了“无效语法”,因为 VS 代码终端中的错误是我的代码

def copy(lst1, lst2 = []):
    if lst1==[]:
        return lst2
    else:
        lst2.append(lst1[0])
        copy(lst1[1:],lst2)                       #recursively calling of copy function

    return lst2
def main():
    lst1= input("Enter the string >> ")
    lst2 = copy(lst1)                             #calling of copy function
    print("The copied list is ", lst2)
main()                                            #calling of main function

【问题讨论】:

    标签: python-3.x list recursion


    【解决方案1】:

    lst1 是一个字符串,lst2 是一个列表。递归的终止条件根据一个空列表检查 lst1。但是空列表和空字符串是不一样的,所以终止条件检查不起作用。

    >>> "" == []
    False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 2016-02-19
      • 2012-03-02
      • 2016-02-10
      • 2021-01-30
      • 2019-12-24
      相关资源
      最近更新 更多