【发布时间】:2019-11-06 17:19:46
【问题描述】:
功能
def find_value(num_list, target):
target_loc = [] # list to store the location of the target
condition = True
while condition == True:
for target in num_list:
if target in num_list:
index = num_list.index(target)
target_loc.append(index)
condition = True
else:
condition = False
return target_loc
主程序:
num_list = keep_positive_numbers()
print()
print("List entered: ", num_list)
print()
target = int(input("Enter target = "))
print()
list = find_value(num_list, target)
print("Target exists at location(s): ", list)
输出
输入一个正整数:9 输入一个正整数:9 输入一个正整数:8 输入一个正整数:0
输入的列表:[9, 9, 8]
输入目标 = 7
目标存在于以下位置:[0, 0, 2]
【问题讨论】:
-
[0, 0, 2]7在[9, 9, 8]中的位置如何? -
m8 idek 我是编程新手。它在列表 [9, 9, 8] 中显示了 9 和 8 的位置,但是它只考虑了 9 的第一个位置。我也刚刚意识到目标是 7,但它给出了元素的位置列表。
-
您的预期输出一般是什么——对于
[9,8,9,10]和9?[0, 2]有机会吗?
标签: python python-3.x list iteration