Question
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started