I’m fairly new to coding, but what this project is asking, is to complete the function so that if a list is empty, it returns the string “does not exist” but currently, it comes up with a bunch of errors. How do I go about adding a function within my lowest_number definition that returns a string if a list is empty (for example, list6)
def lowest_number(num_list): lowest = num_list[0] for x in num_list: if x < lowest: lowest = x return lowest
Advertisement
Answer
This should work. Adding two lines just after you start defining your function.
def lowest_number(num_list): if num_list ==[]: return "does not exist" lowest = num_list[0] for x in num_list: if x < lowest: lowest = x return lowest #given lists list1 = [1, -2, 3] list2 = [1, 2, 3, 0] list3 = [10, 20, 30, 40] list4 = [1.5, 1.2, 1.3] list5 = [7, 191] list6 = [] tests = [list1, list2, list3, list4, list5] for num_list in tests: print("The lowest number in", num_list, "is", lowest_number(num_list)) print("The lowest number in", list6, lowest_number(list6))