Home > database >  I am trying to get output of largest , smallest and the average of all inputs that I enter in python
I am trying to get output of largest , smallest and the average of all inputs that I enter in python

Time:02-27

Write a program that prompts the user for integers. Once the user enters "Done" output the largest, the smallest, and the average (sum/count). If the user enters anything other than an integer, then produce an error message.

largest = -1
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        n = int(num)
    except:
        print("Invalid input")
        
    if smallest is None:
        smallest = n
    elif n < smallest:
        smallest = n 
    elif n > largest:
        largest = n
        
sum = 0
count = 0
sum  = n
count = 1
avg = sum/ count
        
print("Largest: ",largest)
print("Smallest: ",smallest)
print("average: ",avg)

Enter a number: 2 Enter a number: 4 Enter a number: 6 Enter a number: done Largest: 6 Smallest: 2 average: 6.0

CodePudding user response:

In your code, you only sum and count the last number that you received as input. You need the sum and the count variables to be inside of the while loop.

In addition, there are several things I would recommend changing:

  • sum is a built-in function in python. Change that variable to a different name.
  • Change the except to be more specific. In your case, change it to except ValueError. Having only except caches many other things, even a KeyboardInterrupt. See python exception hierarchy.
  • I would change smallest and largest implementation: Currently, if the user just enters "done" your code will print:
Largest: -1
Smallest: None

And then it will return an error as n was not yet defined:

Traceback (most recent call last):
  File "<string>", line 21, in <module>
NameError: name 'n' is not defined

As this is not the desired behavior, you can change your code like so:

input_numbers = []

while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        n = int(num)
        input_numbers.append(n)
    except ValueError:
        print("Invalid input")
   
if not input_numbers:
    print("No numbers received")
else:
    max_input = max(input_numbers)
    min_input = min(input_numbers)
    average_input = sum(input_numbers) // len(input_numbers)

    print("Largest: ", max_input)
    print("Smallest: ", min_input )
    print("average: ", average_input)

So let's break this done:

  • We create the list input_numbers to which we add each of the inputs numbers until we get the "done" input. We can then use the min, max, sum, and len functions to calculate what we need.
  • If the list is empty, that means that the first input we got was "done" - so we print an appropriate message for this edge case.

CodePudding user response:

This is a possible (commented) solution

largest = None
smallest = None

s = 0 # initialize before the loop, also avoid using 'sum' (it's a built-in function)
count = 0 # initialize before the loop

while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        n = int(num)
    except:
        print("Invalid input")
        continue # You need this command to go to the next loop iteration
        
    if smallest is None:
        smallest = n
        largest = n # don't forget to set the largest number too!
    elif n < smallest:
        smallest = n 
    elif n > largest:
        largest = n
    count  = 1 # increment within the loop, every time you read a new number
    s  = n # increment within the loop, every time you read a new number

if count != 0: # you don't want to divide by 0
    avg = s / count

CodePudding user response:

this is my solution :).

numbers = []
while True:
    num = input('Number: ')

    if num == 'done':
        break

    try:
        n = float(num)
        numbers.append(n)
    except:
        print('The input is not a number!')

smallest = None
largest = None
for n in numbers:
    if smallest is None:
        smallest = n 
        largest = n
    
    if n < smallest:
        smallest = n 
    elif n > largest:
        largest = n

average = largest - smallest

print(f'Smallest number: {smallest}')
print(f'Largest number: {largest}')
print(f'Average between numbers: {average}')
  • Related