PLEASE HELP ME FIX THIS CODE. IT DOES NOT SHOW THE LOCATION. Finds the locations
ID: 3702627 • Letter: P
Question
PLEASE HELP ME FIX THIS CODE. IT DOES NOT SHOW THE LOCATION.
Finds the locations of a target value in a list Use: locations = find_value(num_list, target)
def find_value(num_list, target):
locations = []
size = len(num_list)
for index in range(size):
if target == num_list[index]:
locations.append(index)
return locations
num_list = get_string()
print()
print("{}".format(num_list))
target = int(input("Enter the desired target value: "))
locations = find_value(num_list, target)
print()
print("The locations of the target value are: {}".format(locations))
Explanation / Answer
The problem is solved
def find_value(num_list, target): #convert each element in num_list to an integer using list comprehension
locations = []
num_list=[int(i) for i in num_list]
size = len(num_list)
for index in range(size):
if target == num_list[index]:
locations.append(index)
return locations
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.