Skip to content

Instantly share code, notes, and snippets.

@jmangrad
Last active May 25, 2024 18:05
Show Gist options
  • Save jmangrad/8aa25d1cd21b564eb0457114441e5d8b to your computer and use it in GitHub Desktop.
Save jmangrad/8aa25d1cd21b564eb0457114441e5d8b to your computer and use it in GitHub Desktop.
Write another program that prompts for a list of numbers
as above and at the end prints out both the maximum and minimum of
the numbers instead of the average.
largest = None
smallest = None
count = 0
total = 0
while True:
try:
number = input("Enter a number: ")
if number == "done":
break
number = int(number)
count +=1
total =number + total
if largest == None and smallest == None:
largest = number
smallest = number
if largest == None or number > largest:
largest = number
if smallest == None or number < smallest:
smallest = number
except:
print("Invalid input")
print("The maximum number is: {}".format(largest))
print("The minimum number is: {}".format(smallest))
print("The total number is: {}".format(total))
print("The count number is: {}".format(count))
@marzth23
Copy link

I did mine like this, I hope it helps...

smallest = None
largest = None

while True :
    sline = input("Plase enter a number: ")
    if sline == "done" : break
    try:
        fline = float(sline)
    except:
        print("Invalid input")
        continue
    if largest is None or fline > largest : largest = fline
    if smallest is None or fline < smallest : smallest = fline

print('Maximum:',smallest)
print('Minimum:',largest)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment