Write a Python program that takes a list of numbers as input from the user and prints the sum, average, minimum, and maximum of the numbers in the list. 

Answer:

Answer by student

def my_sum (numbers):

  total = 0

  for num in numbers:

    total += num

  return total

 

def my_min (numbers):

  minimum = numbers[ 0 ]

  for num in numbers:

    if num < minimum:

      minimum = num

  return minimum

 

def my_max (numbers):

  maximum = numbers[ 0 ]

  for num in numbers:

    if num > maximum:

      maximum = num

  return maximum

 

user_input = input( "Enter the numbers separated by commas: " )

numbers = [float(num) for num in user_input.split( "," )]

total = my_sum(numbers)

average = total / len(numbers)

minimum = my_min(numbers)

maximum = my_max(numbers)

print( "The sum of the numbers is" , total)

print( "The average of the numbers is" , average)

print( "The minimum of the numbers is" , minimum)

print( "The maximum of the numbers is" , maximum)

Detailed answer by teachoo

-lock-

  • The question asks us to write a Python program that takes a list of numbers as input from the user and prints some statistics about them, such as sum, average, minimum, and maximum.
  • To do this, we need to follow these steps:
    • First, we need to get the user input as a string. We can use the input function for this. We can also prompt the user to enter the numbers separated by commas, so that we can easily split them later.
    • Next, we need to convert the user input into a list of numbers. We can use the split method to break the string into substrings based on a delimiter, which in this case is a comma. Then, we can use a list comprehension to loop over each substring and convert it into a floating-point number using the float function. This will give us a list of numbers that we can store in a variable called numbers .
    • Then, we need to calculate some statistics about the list of numbers. We can write our own functions for this purpose:
      • The my_sum function takes a list of numbers and returns their total. It does this by initializing a variable called total to zero and then looping over each number in the list and adding it to total . Finally, it returns total .
      • The my_min function takes a list of numbers and returns the smallest one. It does this by initializing a variable called minimum to the first element of the list and then looping over each number in the list and comparing it with minimum . If it finds a number that is smaller than minimum , it updates minimum with that number. Finally, it returns minimum .
      • The my_max function takes a list of numbers and returns the largest one. It does this by initializing a variable called maximum to the first element of the list and then looping over each number in the list and comparing it with maximum . If it finds a number that is larger than maximum , it updates maximum with that number. Finally, it returns maximum .
    • To calculate the average of the numbers, we can divide their sum by their length. We can store these values in variables called total , average , minimum , and maximum respectively.
    • Finally, we need to print the results using the print function. We can use string concatenation or formatting to display the values along with some messages.

So, the final code is:

# A function that returns the sum of a list of numbers

def my_sum (numbers):

  # Initialize a variable to store the sum

  total = 0

  # Loop over each number in the list and add it to the sum

  for num in numbers:

    total += num

  # Return the sum

  return total

 

# A function that returns the minimum of a list of numbers

def my_min (numbers):

  # Initialize a variable to store the minimum as the first element of the list

  minimum = numbers[ 0 ]

  # Loop over each number in the list and compare it with the minimum

  for num in numbers:

    # If the number is smaller than the minimum, update the minimum

    if num < minimum:

      minimum = num

  # Return the minimum

  return minimum

 

# A function that returns the maximum of a list of numbers

def my_max (numbers):

  # Initialize a variable to store the maximum as the first element of the list

  maximum = numbers[ 0 ]

  # Loop over each number in the list and compare it with the maximum

  for num in numbers:

    # If the number is larger than the maximum, update the maximum

    if num > maximum:

      maximum = num

  # Return the maximum

  return maximum

 

# Ask the user to enter the numbers separated by commas

user_input = input( "Enter the numbers separated by commas: " )

 

# Convert the user input into a list of numbers

numbers = [float(num) for num in user_input.split( "," )]

 

# Calculate the sum, average, minimum, and maximum of the numbers using our own functions

total = my_sum(numbers)

average = total / len(numbers)

minimum = my_min(numbers)

maximum = my_max(numbers)

 

# Print the results

print( "The sum of the numbers is" , total)

print( "The average of the numbers is" , average)

print( "The minimum of the numbers is" , minimum)

print( "The maximum of the numbers is" , maximum)

-endlock-

Remove Ads
Teachoo · Class 11 Explore Class 11
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.