-4

5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done": 
        break
    print(num)

print("Maximum", largest)

and the output must be like this

Invalid input
Maximum is 10
Minimum is 2

somebody please help me with this????? enter image description here

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
Haider Ali
  • 1
  • 1
  • 2

4 Answers4

1

I think this will do your work,

With simple if conditions

# Defining two variables to None.
largest = None
smallest = None
# starting an infinite loop
while True:
    num = input("Enter a number: ")
# try block to capture ValueError
    try:
        if num == "done":
            break
# assign the values of largest and smallest to num if its None ( on first iteration)
        if largest is None:
            largest = int(num)
        if smallest is None:
            smallest = int(num)
# changing the values of it greater or smaller
        if int(num) > largest:
            largest = int(num)
        if int(num) < smallest:
            smallest = int(num)
# capture the type error and ignores.
    except ValueError:
        print("ignored.")
        continue
print("Maximum: " + str(largest))
print("Minimum: " + str(smallest))

By making use of list and it's methods

# Defining an empty list.
myList = []
# starting an infinite loop
while True:
    num = input("Enter a number: ")
# try block to capture ValueError
    try:
        if num == "done":
            break
# append the entered number to list if valid
        myList.append(int(num))
# catches value error and ignores it
    except ValueError:
        print("ignored.")
        continue
# prints maximum and min
if len(myList) > 0:
    print("Maximum: " + str(max(myList)))
    print("Minimum: " + str(min(myList)))
else:
    print("List is empty")
Jithin Scaria
  • 1,271
  • 1
  • 15
  • 26
1
largest = None
smallest = None
while True:
    try:
        num = input("Enter a number: ")
        if num == "done":
            break
        num = int(num)
        if largest is None or largest < num:
            largest = num
        elif smallest is None or smallest > num:
             smallest = num
    except ValueError:
        print("Invalid input")

print ("Maximum is", largest)
print ("Minimum is", smallest)
David Buck
  • 3,752
  • 35
  • 31
  • 35
Bheemesh
  • 11
  • 1
  • 1
    Welcome to Stack Overflow. Your answer appears to be only fractionally different to that posted by Ernest a year ago, but is functionally identical. Given that this is a question to solve someome's homework in 2018, and that you haven't explained why you think your solution improves Ernest's, it's probably not really worth posting. – David Buck May 23 '20 at 18:23
  • 1
    Hope It will solve issue but please add explanation of your code with it so user will get perfect understanding which he/she really wants. – Jaimil Patel May 23 '20 at 18:32
0
largest = None
smallest = None

while True:
    num = input("Enter a number: ")
    if num == "done" :
        break

    try:
        n = int(num)
    except:
        print ('Invalid input')
        continue


    if largest is None or n > largest:
        largest = n
    if smallest is None or n < smallest:
        smallest = n

print("Maximum is", largest)
print("Minimum is", smallest)
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
-1
#Print out largest and smallest number
largest=None
smallest=None
while True:
    n = input('Enter a number: ')
    if n == "done":
        break    
    try:
        num=int(n)
        if largest is None:
            largest = num
        elif num > largest:
            largest = num

        if smallest is None:
            smallest = num
        elif num < smallest:
            smallest = num
    except:
        print("Invalid input")
    print('maximum:', largest)
    print('Minimum:', smallest)
Andreas
  • 2,455
  • 10
  • 21
  • 24