gpt4 book ai didi

python - 如何获取列表中的索引

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:54 26 4
gpt4 key购买 nike

如何检查是否找到该值并打印语法?我已经完成了所有代码,但我的问题是我需要找到该值在列表中的位置。问题出在代码的末尾。我将 #blank 放在这些区域中,因为我不知道该放什么。

这是我的代码:

list_of_scores = []

count = 0

for i in range(10):

grade = int(input("Enter a quiz score "))
list_of_scores.append(grade)

for grade in list_of_scores:

while(count < 10):
print("The score at position[", count, "] = ", list_of_scores[count])
count = count + 1

search_value = (int(input("Enter a number to search for: ")))

***下面是我遇到麻烦的地方。我的代码的顶部部分应该是这样的:

while(count < len(list_of_scores)):
if(search_value) in list_of_scores:
print("The value of ", search_value, "is in the list at index",#blank)
else:
if search_value not in list_of_scores:
print(input("Sorry, this value isn't found. Please enter a new number:"))

print("The location of this value is: ", list_of_scores)

最佳答案

使用index()获取列表中项目的索引

print("The value of ", search_value, "is in the list at index",list_of_scores.index(search_value))

关于python - 如何获取列表中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245268/

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