''' DS2000 Spring 2023 Sample code from class - 1. writing our own functions 2. calling functions ''' import random import matplotlib.pyplot as plt def max_list(lst): ''' Function: max_list Parameters: list of non-negative numbers Returns: a number Does: finds and returns the max value in the list ''' # this would cause an error, no "mylst" in max_list # print(mylst) curr_max = -1 for num in lst: if num > curr_max: curr_max = num return curr_max # The line below will never happen :( # "return" both returns the result and ends the function print("Function is over") def plot_list(lst): ''' Function: plot list Parameters: list of numbers Returns: nothing! Does: Plot the values in the list ''' plt.plot(lst) plt.xlabel("position") plt.ylabel("random value") plt.title("Randomly generated numbers") def main(): # Gather data - generate a list of random numbers mylst = [random.randint(0, 50) for i in range(50)] # this would cause an error, no "lst" in main # print(lst) # Computation - find the max by calling the max function max_value = max_list(mylst) # Communication - print out the original list and the max we found # and call the plot function to plot the list print(mylst) print("Max value is:", max_value) plot_list(mylst) main()