gpt4 book ai didi

python - 索引错误 : list index out of range in array search

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:19 25 4
gpt4 key购买 nike

<分区>

我正在用 Python 编写一个简单的程序来执行线性搜索。但是当我运行这个程序时,它给了我这个错误:

Traceback (most recent call last):
File "C:\Users\raj\Documents\EclipseProject\PythonProject1\myProgram.py", line 25, in <module>
if array[myNumber] == search:
IndexError: list index out of range

这是我的程序:

array = []

numCase = input("Enter your number cases: ")

numCase = int(numCase)
array = [numCase]

print("Enter the number with", numCase, "times.")

for i in range(0, numCase):
myNumber = input()
myNumber = int(myNumber)
array = [myNumber]

print("Enter the values which you're looking for: ")
search = input()
search = int(search)

for c in range(0, numCase):
if array[myNumber] == search:
print(search, "is present at", (c+1))
break

# If number is absent!!
if c == numCase:
print(search, "is not present!!")

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com