【问题标题】:List index is out of rage in search algorithm列表索引在搜索算法中过时了
【发布时间】:2019-04-30 07:58:49
【问题描述】:

我正在尝试找出我的代码中的问题:
https://repl.it/@lerowhou/VerifiablePunyRobots

你能看一下吗?

【问题讨论】:

标签: python python-3.x algorithm search


【解决方案1】:
arr = [1,2,3,4,5,6,7,8,9,10]
n = int(input())

if n in arr:
    print("The element is there and it's position is %d" %arr.index(n))
else:
    print("The element is not in the list")

【讨论】:

  • 我猜当您输入大于 17 的数字时会发生错误?
  • 是的,你是对的。而且我真的不知道错在哪里
  • 它是因为while循环,当你把while设为True时,它会进入一个无限循环,并且由于你的程序列表长度是16,一旦它到达第16个位置,它就说你的列表已经出来了范围
  • 实际上python的设计是这样的,你不需要搜索这样的元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 2011-09-04
  • 2018-03-24
  • 2013-07-28
  • 2012-04-16
  • 1970-01-01
相关资源
最近更新 更多