繁体   English   中英

如何在列表中获取索引

[英]How to get index in list

如何检查是否找到该值并打印语法? 我已经完成了所有代码,但是我的问题是我需要定位该值在列表中的位置。 问题在代码末尾。 我将#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(“此值的位置为:”,list_of_scores)

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

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

暂无
暂无

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

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