63.7k views
0 votes
Let us build on the solution that we previously created and write a program that takes any number of values as input, and outputs the average, min, and max only if the input values were all numeric Ex If the input is 15 20 0 5 the output is 100.20 Use (1.0f) to format the output of the computed average Define and use in your program the following functions to make your code more modular • convert_str_to_numeric_1int-taken an input string, splits it into tokens, and returns the tokens stored in a list on all tokens were numeric, otherwise, returns an empty list • get_avg-if the input list is not empty and stores only numerical values, returns the average value of the elements, otherwise, returns • get_min if the input list is not empty and stores only numerical values, returns the minimum value in the list otherwise, returns • get_max of the input list is not empty and stores only numerical values, returns the maximum value in the list otherwise, returns None None None Hint. The string method atr. indigit() will help determine if an input string contains only the numbers 0-9 (see the Section on String mathadh

1 Answer

1 vote

Answer:

Following are the code to these question:

def convert_str_to_numeric_list (numbers):#defining a method convert_str_to_numeric_list that holds number as a parameter

numbers_list = numbers.split ()#defining a list that hols list value by using splits method

for i in range (len (numbers_list)):#use loop for calculate the length of list

if (numbers_list[i].isdigit ()):#use if that check digits in list

numbers_list[i] = int (numbers_list[i])#convert list value into numbers

else:#else block

return[] #use return to return list v

return numbers_list #returning list

def get_avg (numbers_list):#defining a method get_avg that holds list in parameter

if (len (numbers_list) != 0):#use if to check lenght of the list is not equal to 0

return sum (numbers_list) / len (numbers_list)#use return to calculate average of the list

else: #use else block

return None #return None value

def get_min (numbers_list):#defining get_min method that holds list in parameter

if (len (numbers_list) != 0):#use if to check lenght of the list is not equal to 0

return min (numbers_list) #use return to calculate the minimum element in list

else: #use else block

return None #return None

def get_max (numbers_list):#defining get_max method that holds list in parameter

if (len (numbers_list) != 0): #use if to check lenght of the list is not equal to 0

return max (numbers_list) #return minimum number in the list elements

else:# use else block

return None #return None

numbers = input () #defining numbers that use input method to input value

numbers_list = convert_str_to_numeric_list (numbers) #use list that pass hold the method value

average = get_avg (numbers_list)#defining average variable that calls get_avg method

minimum = get_min (numbers_list)#defining minimum variable that calls get_min method

maximum = get_max (numbers_list)#defining maximum variable that calls get_max method

if (len (numbers_list) != 0):#if to check lenght of the list is not equal to 0

print ("{:.0f} {} {}".format (average, minimum, maximum)) #print calculated values.

Output:

15 20 0 5

10 0 20

Step-by-step explanation:

In this code, a method "convert_str_to_numeric_list" is declared that holds a numbers variable as a parameter and inside this, a list is declared that

that splits the lists and in the for loop, it checks the length of the array and converts the list value into the numbers, and returns its values.

In the next step, "get_avg, get_min, and get_max" three methods are declared, that check the length of the list and return its respective values.

At the last, the numbers are declared that inputs values and passes into the list and use "average, minimum, and maximum" variable to call the method holds its value and at the last, it uses the print method to print its values.

User Teedyay
by
3.0k points