dimanche 17 septembre 2023

Compare a random generated list (list 2) with another list (list 1) and print the value from list 2 and the corresponding index in list 1

l) Use a for loop and a random integer generator to generate 5 random integers in the range specified by the user in step a. Check if each number is in the combined list. • If the number is in the combined list, print the number and print the index of the first element that matches. • NOTE: There is a list method for finding an element value which return the index and you should use that function. Do NOT use a for loop or you will lose some points. • If the element is found, after printing the index, the element should be deleted or removed from the list. • NOTE: There is both a function and a list method that can delete or remove an element from a list. You should use either the function or the list method. Do NOT use a for loop or you will lose some points. • If the number is not in the combined list, print the number followed by "not found in list".

^These are the instructions for the problem

```def main():
entries = int(input('Step a: How many numbers in each list? '))
lower_bound = int(input('Step b: \nWhat is the lower bound for the random numbers? '))
upper_bound = int(input('What is the upper bound for the random numbers? '))
list_one = []
for i in range(entries):
    list_one.append(random.randint(lower_bound, upper_bound))
print(f'Step c: First list: {list_one}')
list_two = create_second_list(entries, lower_bound, upper_bound)
print(f'Step d: Second list: {list_two}')
print('Step e:')
for i in range(entries):
    print(list_one[i], list_two[i])
list_combined = list_one + list_two
print(f'Step f: Combined list:', list_combined)
list_combined.sort()
print(f'Step g: Sorted list:', list_combined)
print(f'Step h: \nFirst three elements: {list_combined[0:3]}\n'
      f'Last three elements: {list_combined[-3:]}')
sum_elements(list_combined)
min_elements(list_combined)
max_elements(list_combined)
print('Step l:')
delete_list = []
for x in range(5):
    delete_list.append(random.randint(lower_bound, upper_bound))
    if delete_list ```

I've tried making an if statement but instead of printing the delete_list it printed 0-4 and the corresponding index in my combined_list




Aucun commentaire:

Enregistrer un commentaire