Study Guide

8.3.5 Max In List CodeHS Answers

8.3.5 Max In List CodeHS Answers

/The most common answer is:

your function should return the maximum value in my_list
my_list = [1, 3, 456, 2038]
def max_int_in_list(my_list):
highest = my_list[-1]
for num in my_list:
if num > highest:
highest = num
return highest
biggest_int = max_int_in_list(my_list)
print (biggest_int)

The max_int_in_list function is correctly set up to find and return the maximum value in my_list. The function initializes highest with the last item of the list and iterates through my_list to find the maximum value.

Here is the code, correctly formatted:

my_list = [1, 3, 456, 2038]

def max_int_in_list(my_list):
    highest = my_list[-1]
    for num in my_list:
        if num > highest:
            highest = num
    return highest

biggest_int = max_int_in_list(my_list)
print(biggest_int)

This approach correctly identifies 2038 as the maximum value in the list [1, 3, 456, 2038].