Varied amount of input data; Needing advice

Question:

Statistics are often calculated with varying amounts of input data. Write a program that takes any number of integers as input, and outputs the average and max.

Ex: If the input is:

15 20 0 5
the output is:

10 20

input_number = input()
split_number = input_number.split()

sum = 0
max = -1

for n in split_number:
    sum = sum + int(n)

average_value = sum/len(split_number)
if (int(n) > max):
    max = int(n)

print(int(average_value), max)

I tried this and ending up with 15 15 not 10 20

Asked By: Emily

||

Answers:

Indenting is crucial here

You need the comparison with max to be done for each number, not just at the end.

A few extra tips:

  • Don’t overwrite the names sum and max which are built-in functions. The easiest way to avoid this is to use a longer name which doesn’t collide with a built-in name, e.g. total and maximum.

  • Avoid repeatedly inting the string n. Instead, get it into a number once-and-for-all. Since you only need the numerical part of the input, get the input converted into a list of numbers immediately.

  • A handy mnemonic technique is to use a singular noun like "number" for one number, and a plural noun like "numbers" for a list of numbers. That way all your for loops will have a stereotyped layout: for thing in things:

  • Avoid naming a variable input_number if it is actually a string, and it contains multiple numbers (expressed as strings). A few years later when you read the program, you might wrongly believe that input_number is a number, or if you remember that it is a string, you might think it is a string form of a single number.

text = input()
numbers = [int(number) for number in text.split()]

total = 0
maximum = -1

for number in numbers:
    total = total + number
    if (number > maximum):
        maximum = number

average_value = total/len(numbers)

print((average_value), maximum)
Answered By: ProfDFrancis

There are a few points you have to consider.

  1. You should not use variable names like max or sum as these are built in functions. I have replaced them with max_num and sum_num respectively.
  2. You have to put the if statement inside the for loop.
  3. You should not cast average_value to an integer as it will reduce precision.

Modified code:

input_number = input()
split_number = input_number.split()

sum_num = 0
max_num = -1

for n in split_number:
    sum_num += int(n)

    if int(n) > max_num:
        max_num = int(n)

average_value = sum_num/len(split_number)

print(average_value, max_num)
Answered By: Dinux

Try looking at the python builtin functions: sum, max, and statistics.mean.

In order to do this, you first need a list of ints, not strs. You can use a simple list comprehension to get that done like so:

numbers = [int(number) for number in input().split()]

After you have your list of numbers, in order to get the maximum number, you can use max(numbers). In order to get the mean average, you can use mean(numbers) (note: you must have imported mean from statistics before to use this). Finally, to get the sum of the numbers, just use sum(numbers).

In order to get your desired output, this is what I would write:

from statistics import mean
numbers = [int(number) for number in input().split()]

average = mean(numbers)
largest_number = max(numbers)

print(average, largest_number)

If, instead you do not want to import statistics, you can just use sum(numbers) instead (note: as you are using division this will always return a float whereas mean() might not):

numbers = [int(number) for number in input().split()]

average = sum(numbers) / len(numbers)
largest_number = max(numbers)

print(average, largest_number)
Answered By: xihtyM
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.