Question: Python/Pygame- Write a function called find that will take a list of numbers, my_list, along with one other number, key. Have it search the list
Python/Pygame- Write a function called find that will take a list of numbers, my_list, along with one other number, key. Have it search the list for the value contained in key. Each time your function finds the key value, print the array position of the key. You will need to juggle three variables, one for the list, one for the key, and one for the position of where you are in the list.
This code will look similar to the Chapter 7 code for iterating though a list using the range and len functions. Start with that code and modify the print to show each element and its position. Then instead of just printing each number, add an if statement to only print the ones we care about.
Copy/paste this code to test it:
| my_list = [36, 31, 79, 96, 36, 91, 77, 33, 19, 3, 34, 12, 70, 12, 54, 98, 86, 11, 17, 17] find(my_list, 12) find(my_list, 91) find(my_list, 80) |
...check for this output:
| Found 12 at position 11 Found 12 at position 13 Found 91 at position 5 |
Use a for loop with an index variable and a range. Inside the loop use an if statement. The function can be written in about four lines of code.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
