Question

In: Computer Science

Write a Python function that will return the index of an element in a list.1- The...

Write a Python function that will return the index of an element in a list.1- The function will receive a list of at least 5 numbers as a single argument when the function is called. 2. The function will ask the user to input a number and will find and return the index of that number in the list.3. If the number is not an element of the list, the function returns the string "unknown."

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 


If you are satisfied with the solution, please rate the answer. 

Thanks!
===========================================================================

def getIndex(lst):
    # The function will ask the user to input a number
    number = int(input('Enter a number: '))
    # iterate over each element in the list
    for i in range(len(lst)):
        # if the current index number matches user number
        if lst[i] == number:
            return i  # return the index
    # if there are no matches return 'unknown'
    return 'unknown'


def main():
    lst = [1, 2, 3, 45, 6]
    index = getIndex(lst)
    print('Index:', index)


main()

===================================================================


Related Solutions

PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
Write a function argmax(somelist=[]) to return the index of the largest number in a numerical list,...
Write a function argmax(somelist=[]) to return the index of the largest number in a numerical list, or None if the list is empty. If there are ties, then return the index of the first occurrence of the largest number. Sample: argmax([2, 2, 4, 4, 1]) returns 2. in python program
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
In Python, Complete the longestWord() function to take a list as a parameter and return the...
In Python, Complete the longestWord() function to take a list as a parameter and return the length of the longest word in that list. Examples: longestWord(['Python', 'rocks']) returns 5 longestWord(['Casey', 'Riley', 'Jessie', 'Jackie', 'Jaime', 'Kerry', 'Jody']) returns 6 longestWord(['I', 'a', 'am', 'an', 'as', 'at', 'ax', 'the']) returns 3
haskell : write a function that reverse the first three element of a list, but not...
haskell : write a function that reverse the first three element of a list, but not the rest. example [1,2,3,4,5,6] == [3,2,1,4,5,6]
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
for input matrix a , write a function to return the element in row 2 column...
for input matrix a , write a function to return the element in row 2 column 3 of that matrix
Python: I am currently a function that will return a list of lists, with the frequency...
Python: I am currently a function that will return a list of lists, with the frequency and the zip code. However, I am having a difficult time organizing the list in decreasing order of frequency. We are asked to use sort () and sorted () instead of lambda. The function will find 5 digit zip codes, and when it sees a 9-digit zip code, the function needs to truncate the last 4 digits. The following is an example of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT