2

I need to write a program where you can only input 10 numbers, and from those 10 numbers it then will print the minimum number out of the 10 inputted numbers and then print average of all the numbers.

Instructions: Write a program that reads 10 integers and displays its minimum and average.

What I have so far:

c=1
min=int(input("a number>1: "))
while c<10:
    v=int(input("a number>2: "))
    print (min)
    print (v)
    if min>v:
        min=v
    c += 1
    d = sum(int(min+v)
        print (d)
        print ("Minimum number: " + str(min))

Or this:

a = 0
b = int(input("a number>1: "))
while a < 10:
    c = int(input("a number>1: "))
    d = int(input("a number>1: "))
    e = int(input("a number>1: "))
    f = int(input("a number>1: "))
    g = int(input("a number>1: "))
    h = int(input("a number>1: "))
    i = int(input("a number>1: "))
    j = int(input("a number>1: "))
    k = int(input("a number>1: "))
    a += 1
    if (b>c and b<d and b<e and b<f and b<g and b<h and b<i and b<j and b<k):
        print ("Minimum is" + str (b))
 #   c =
 #   a += 1

#print(min)
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Matt
  • 31
  • 3

2 Answers2

2

If you find yourself writing really annoying repetitive code like:

if (b>c and b<d and b<e and b<f and b<g and b<h and b<i and b<j and b<k):

you should step back and ask yourself if there is a better way. Sometimes you have to write annoying code, but for something like finding a minimum value or sum…well, you have to know python programmers aren't doing this every time…there's got to be a better way.

So if you have a list of numbers like [1, 2, 3] rather than bunch of variables like a = 1; b = 2 you can simply use min(list) to find the smallest. So instead of defining all these variables use a data structure like a list and for each input append() to the list. In the end you'll have a tidy list of 10 numbers to work with and a large set of tools python gives you like len(), min(), sum() etc.

numbers = []                               # put you numbers in a list
while len(numbers) < 10:            
    i = int(input("a number>1: "))
    numbers.append(i)

print("Numbers: ", numbers)
print ("Minimum is: %d " % min(numbers))  # then you can call min and sum
print ("Sum is: %d" % sum(numbers))

Of course there are a lot of ways to do this.

From here I assume you could figure out the average.

Mark
  • 90,562
  • 7
  • 108
  • 148
0

You can create two variables; one that will hold the sum of the numbers (in which at the end, you just divide that number by 10) and another that will hold the minimum value.

We will set the min and sum to the first number then create a loop to obtain the next 9 numbers using the function range(start, end) which would count from start to end-1 (whatever value you pass as the second parameter, will not be included in the loop).

Then in the loop, you can allow the user to enter the number in which you will: add it to the sum variable and set it has the minimum if it is less than the current value of the min variable.

min = int(input('Enter a number > 1: '))
sum = min

for i in range(0, 9):
    number = int(input('Enter a number > 1: '))
    sum += number

    if number < min:
        min = number

average = sum / 10

print('Minimum:', str(min))
print('Average:', str(average))
Omari Celestine
  • 1,405
  • 1
  • 11
  • 21