7

I have to ask the user to put some numbers and then print the size, sum, average, minimum and maximum. I can get the first 3 things but I'm stuck on the minimum and maximum one. The problem I have is I can't use sort() because I need to make the list an integer one, but you can't use an integer list for split()

Here's my code:

    number = raw_input('Enter number:')
    list_of_numbers = number.split()
    tally = 0
    sum = 0
      while number!= '':
        tally = tally + 1
        sum = sum + int(number)
        average = float(sum) / float(tally)
      number = raw_input('Enter number:')
    print "Size:", tally
    print "Sum:", sum
    print "Average:", average

Any hints? Thanks

6 Answers 6

18

Are you allowed to use built-in functions of Python? If yes, it is easier:

number = raw_input('Enter number:')
list_of_numbers = number.split()

numbersInt = map(int, list_of_numbers) # convert string list to integer list

print("Size:",    len(numbersInt))
print("Min:",     min(numbersInt))
print("Max:",     max(numbersInt))
print("Sum:",     sum(numbersInt))
print("Average:", float(sum(numbersInt))/len(numbersInt) if len(numbersInt) > 0 else float('nan'))
# float conversion is only required by Python 2. 

where numbersInt = map(int, list_of_numbers) converts each string number of the list to an integer. Each function has the following meaning:

  • len computes the length of a list
  • min computes the minimum
  • max computes the maximum
  • sum computes the sum of a list

There isn't a mean function in Python standard library. But you can use numpy.mean() instead. Install it with pip install numpy or conda install numpy, then:

import numpy as np
print("Average: ", np.mean(numbersInt))
Sign up to request clarification or add additional context in comments.

1 Comment

This is most pythonic way and most efficient one.
4

I think you can use min() and max() to find those values.

Comments

2

You have two options:

  • You could compare the current number to the previous extrema and update your minimum and maximum accordingly;

    import sys
    tally = 0
    sum = 0
    nmax = -sys.maxint
    nmin = +sys.maxint
    number = raw_input('Enter number:')
    while number!= '':
        number = int(number)
        if number > nmax:
            nmax = number
        elif number < nmin:
            nmin = number
        tally = tally + 1
        sum += number
        average = float(sum) / float(tally)
        number = raw_input('Enter number:')
    

    We use the largest integer on your system (sys.maxint) to initialize our nmin and nmax

  • You could store each number in a list, then perform all the operations at once:

    number = raw_input('Enter number:')
    numbers = []
    while number!= '':
        numbers.append(int(number))
        number = raw_input('Enter number:')
    numbers.sort()
    nmin = numbers[0]
    nmax = numbers[-1]
    tally = len(numbers)
    nsum = sum(numbers)
    avg = nsum/float(tally)
    

Comments

1

This may be with least complexity if I am not wrong.

len_min_max_sum=reduce(lambda x,y : (x[0]+1,y,x[2],x[3]+y) if x[0]>y else (x[0]+1,x[1],y,x[3]+y) if x[2]<y else (x[0]+1,x[1],x[2],x[3]+y) ,arr,(0,arr[0],arr[0],0))

len_min_max_sum_avg=(len_min_max_sum[0],len_min_max_sum[1],len_min_max_sum[2],len_min_max_sum[3],float(len_min_max_sum[3])/len_min_max_sum[0])

Comments

1
tally = 0
sum = 0
average = 0
while (True):
    number = input('Enter number')
    list_of_numbers = number.split()
    if number == '':
        continue
    else:
        tally = tally + 1
        sum = sum + int(number)
        average = float(sum)/float(tally)
    print('Size:', tally)
    print('Sum:', sum)
    print('Average:', average)

1 Comment

Please edit your indentation. It is crucial for Python.
0

The max() and min() predefined math methods in python can solve this as below

numbers = [3,2,5,9,5,3]
minimum = min(numbers)
maximum = max(numbers)

result of the above is

>>>maximum
9
>>>minimum
2

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.