Step 1 and 2 are steps to check that your program is working. If either of these steps fail, step 3 and 4 will be helpful in locating the problem. If you fail because python crashes, it is also helpful to read the error message printed. It even tells you which line the error is on.
Place these files in a lab5 folder. Before leaving lab, zip up the lab5 folder and hand in the zip file.
Show a program that has a syntax error and show how to read the error message to find the line where the error occurs.
The function contains(list,key) should return True if a list contains the key and False otherwise.
def contains(list, key): for item in list: if item == key: print(True) print(False)
Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in contains.py.
The function get_index(list,key) should return the first index of the key in the list. It should return -1 if the key is not in the list. (Note: In this case, we want to return -1 if the key is not found. Returning -1 is not the bug.)
def get_index(list, key): for i in range(0, len(list)): if list(i) == key: return i return -1
Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in get_index.py.
The function square_evens(num_list) should square all the even numbers in num_list and then print the result.
def square_evens(num_list): for num in num_list: if num % 2 == 0: num = num ** 2 print(num_list)
Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in square_evens.py.
The pow(base,exp) is the power (exponentiation) function, defined recursively. It should raise the base to the power of the exponent. In other words, it should be equivalent to base ** exp. However, this function must work recursively; you can't just correct it by returning base ** exp to solve this problem.
To compute baseexp recursively, we note that
baseexp = base × baseexp - 1
with the special case that
baseexp = base, if exp = 1.
def pow(base, exp): return base * pow(base,exp - 1) if exp == 1: return base
Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in pow.py.
The function list_mult(list, multiplier) should multiply all of the numbers in the given list by the given multiplier. The function test_mult(list) should use list_mult to create 2 different modifications of the given list: two_mult should be a list made up of all of the original list elements multiplied by 2, and five_mult should be a list made of all of the original list elements multiplied by 5.
def list_mult(list, multiplier): for i in range(0,len(list)): list[i] = list[i] * multiplier return list def test_mult(list): two_mult = list_mult(list, 2) five_mult = list_mult(list, 5) print(two_mult) print(five_mult)
Determine the bug(s), write the answers to the questions above in your answers file, and store the corrected version of the function in list_mult.py.