Skip to content

Instantly share code, notes, and snippets.

@AmruthPillai
Created August 21, 2017 14:30
Show Gist options
  • Save AmruthPillai/77026640f8be299fd3d61e740034c1fa to your computer and use it in GitHub Desktop.
Save AmruthPillai/77026640f8be299fd3d61e740034c1fa to your computer and use it in GitHub Desktop.
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.
"""
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.
"""
largest = None
smallest = None
while True:
try:
num = raw_input("Enter a number: ")
if num == 'done':
break;
n = int(num)
largest = num if largest < num or largest == None else largest
smallest = num if smallest > num or smallest == None else smallest
except:
print "Invalid input"
print "Maximum number is ", largest
print "Minimum number is ", smallest
@VinayReddy-Vangala
Copy link

@kreeztoph hi ur code seems fine but u have used list keyword instead name it list1 /list2 and try it .
largest = None
smallest = None
List_collector=[]
while True:
try:
num = input("Enter a number: ")
if num == 'done':
break
n = int(num)
List_collector.append(n)
largest=max(List_collector)
smallest=min(List_collector)
except:
print("Invalid input")
continue

print("Maximum is", largest)
print("Minimum is", smallest)

@akatsiou
Copy link

akatsiou commented Mar 1, 2022

The below worked for me!

largest = None
smallest = None

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

try: 
    inum = int(num)
except:
    print('Invalid input')
    continue
    
if largest is None or inum > largest:
	largest = inum
elif smallest is None or inum < smallest:
    smallest = inum   

print ("Maximum is",largest)
print ("Minimum is",smallest)

@sofiamoj
Copy link

sofiamoj commented Apr 8, 2022

largest = None
smallest = None
while True:
try:
num = raw_input("Enter a number: ")
if num == "done":
break
num = int(num)
if largest is None or largest < num:
largest = num
elif smallest == None or smallest > num:
smallest = num

except:

Annotation 2022-04-08 162026

    print("Invalid input")
    continue
    
#print(num)

print("Maximum is", largest)
print("Minimum is", small
Annotation 2022-04-08 162026
est)

@MSThedox
Copy link

Working

largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
n = int(num)
if largest is None or n > largest:
largest = n
elif smallest is None or n < smallest:
smallest = n
except Exception:
print("Invalid input")
continue

print("Maximum is", largest)
print("Minimum is", smallest)

Invalid input

Maximum is 10

Minimum is 2

@PersonalDvbt22
Copy link

image
Please help Unable to get correct Output

@toyeade1
Copy link

toyeade1 commented Jun 1, 2022

Hey PersonalDvbt22 it might serve you well to separate the try/except functions from the conditional loop for the smallest and largest. Remember that the try should only be inputted for items that are absolute. Try this instead:
Screen Shot 2022-05-31 at 8 04 45 PM

@Aliqasim222
Copy link

Working
12222
largest = None
smallest = None
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
try:
num = int(num)
except:
print("Invalid input")
continue

if largest is None:
    largest = num
elif num > largest:
    largest = num
if smallest is None:
    smallest = num
elif num < smallest:
    smallest = num

print("Maximum is", largest)
print("Minimum is", smallest)

@Faythly
Copy link

Faythly commented Jun 26, 2022

In need help
Uploading image.jpg…

@harshrao7446
Copy link

list = []
while True:
try:
number = input("Enter a number:")
if number == 'done':
break
converted = int(number)
list.append(converted)
maximum = max(list)
minimum = min(list)

except:
    print("Invalid input")

print("Maximum is",maximum)
print("Minimum is",minimum)

@MaryamFarshbafi
Copy link

largest = 0
smallest = 0
while True:

try:
num = raw_input("Enter a number: ")
if num == "done":
break
num = int(num)
if largest == 0 or largest < num:
largest = num
elif smallest == 0 or smallest > num:
smallest = num
except:
print("Invalid input")

print("Maximum is", largest)
print("Minimum is", smallest)

@ltnr0312
Copy link

ltnr0312 commented Aug 23, 2022

error encounering invalid error 5 times anyone can explain why?

I think your if condition is something wrong.

largest = None
smallest = None
while True:
    try:
        num = raw_input("Enter a number: ")
        if num == "done":
            break
        n = int(num)
        if largest is None or n > largest:
            largest = n
        elif smallest is None or n < smallest:
            smallest = n
    except ValueError:
        print("Invalid input")
        continue
print("Maximum is", largest)
print("Minimum is", smallest)

@smukasa90
Copy link

largest = None
smallest = None
fig = True # will help to initialize largest and smallest from None to a known number

while True:
num = input("ENter a number: ")

if num == "done":
    break

try:
    num = int(num)

   if fig is True:
        largest = num
        smallest = num
        fig = False
    else:
        if (num >= largest):
            largest = num
        elif (num <= smallest):
            smallest = num
except:
    print("Invalid input")

print ("Maximum", largest)
print ("Minimum", smallest)

@Faythly
Copy link

Faythly commented Sep 2, 2022 via email

@Ben7455
Copy link

Ben7455 commented Sep 17, 2022

Screenshot 2022-09-17 5 04 05 PM
why am i getting this feedback

@WestonSpiro
Copy link

largest = None
smallest = None

while True:
try:
num = input("Enter a number: ")
if num == 'done':
break
n = int(num)
if largest is None or n > largest:
largest = n
elif smallest is None or n < smallest:
smallest = n

except:
    print("Invalid input")
    continue

print("Maximum is", largest)
print("Minimum is", smallest)

@JoseA8890
Copy link

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 largest < n:
        largest = n
    elif smallest is None or smallest > n:
        smallest = n
    elif n < smallest:
        smallest = n

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

Thanks a lot Man It worked out

Quick question why does this work when comparing to a NoneType value but when comparing it directly won't work? I understand they are different types of data and can't be compared but why adding that extra "is None" makes it work.

@candzon
Copy link

candzon commented Nov 4, 2022

Hi man, try this code

numList = []
while True:
        try:
            num = input("Enter a number: ")
            if num == 'done':
              break
            n = int(num)
            if n != 'done':
                Value = n
                numList.append(Value)
                smallest = min(numList)
                largest = max(numList)
        except:
            print("Invalid input")
            continue
 
print("Maximum is", largest)
print("Minimum is", smallest)

image

@sandykrishdaswani
Copy link

Hi man, try this code

numList = []
while True:
        try:
            num = input("Enter a number: ")
            if num == 'done':
              break
            n = int(num)
            if n != 'done':
                Value = n
                numList.append(Value)
                smallest = min(numList)
                largest = max(numList)
        except:
            print("Invalid input")
            continue
 
print("Maximum is", largest)
print("Minimum is", smallest)

image

@DataHawker
Copy link

DataHawker commented Dec 11, 2022

You can try this:

num_set = []

def check_num():
    while True:
        try:
            num = input("Enter a number or type 'done': ")
            lower_num = num.lower()
            if lower_num == "done":
                return
            isinstance(num, int)
            num = int(num)
            num_set.append(num)
        except:
            print("Invalid input")
       
def check_size():
    if not num_set:
        return  
    else:
        print("Maximum is", max(num_set))
        print("Minimum is", min(num_set))
                               
check_num()
check_size()

@mehak2602
Copy link

This should work

largest = None
smallest = None

while True :
    number = input('Enter Number:')
    if number == 'done' :
        break

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

    if smallest is None :
        smallest = num
    elif num < smallest :
        smallest = num

    if largest is None :
        largest = num
    elif num > largest :
        largest = num
        
print('Maximum is', largest)
print('Minimum is', smallest)```

@tahira2k16
Copy link

largest = None
smallest = None
#MOST NEEDED COMMAND AS IT MADE ME CONFUSED TOO
#print("input Done to end loop")
while True:
try:
num= input("input number")
if num == "done":
break
num = int(num)
if largest is None or num > largest:
largest = num
elif smallest is None or num < smallest:
smallest = num

except:
    print("Invalid input")
    continue

print("Maximum is", largest)
print("Minimum is", smallest)

@ShuckZ77
Copy link

**smlest = None
lrgest = None

while True:
num = input('ENTER THE NUMBER :')

if num == 'done':
    break
    
try:
    n=int(num)
    
except:
    print('Invalid input')
    
if smlest is None:
    smlest=n
elif n<smlest:
    smlest=n
elif lrgest is None:
    lrgest=n
elif n>lrgest:
    lrgest=n

print('Maximum is',lrgest)
print('Minimum is',smlest)**

@najumuddeen
Copy link

largest = None
smallest = None
while True :
try :
num = raw_input("Enter number")
if num == "done" :
break
num = int(num)
if largest is None :
largest = num
elif largest < num :
largest = num
if smallest is None:
smallest = num
elif smallest > num :
smallest = num
except :
print("Invalid input")
continue
print("Maximun is", largest)
print("Mininimum is", smallest)
Capture

@facundolubo
Copy link

facundolubo commented May 28, 2023

""" 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.

My inputs: 7, 2, bob, 10, and 4
My outputs: Invalid input, 10, 10
"""

largest = None smallest = None

while True:
try:
num = input("Enter a number: ")

""" In order to avoid the type error of the comparison between integer and NoneType you must 'cast' the integer type in the if/else statement. `Otherwhise` the first time you would compare an integer with a nonetype. """

largest = int(num) if largest == None or int(largest) < int(num) else largest smallest = int(num) if smallest == None or int(smallest) > int(num) else smallest except:

""" You need to enter here only if the command "int(num)" throws an error. Then, you ask if num is a string with the value 'done' in order to break the loop """

if str(num) == 'done': break; print("Invalid input")

print("Maximum is", largest)
print("Minimum is", smallest)

@probleo
Copy link

probleo commented Jun 29, 2023

Check out my answer:

"""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."""


# create an empty list where the user input will be stored.
num_list = []

while True:
    userNumber = input("Please enter a number. Type 'Done' if you want to stop: ")
    if userNumber == 'Done':
         break
    try:
        userNumberint = int(userNumber)
    except:
        print("Invalid input")
        continue

    # the user's input will be appended in num_list.    
    num_list.append(userNumberint)

# added an if statement to check if the list is empty.
if bool(num_list):   
    # If the list is not empty, it will print the max and min numbers.
    print(f"Maximum is {max(num_list)}")
    print(f"Minimum is {min(num_list)}")
# If the list is empty, it will print the below statement.
else:
    print("Please type some numbers.")

@MUNIBA-12
Copy link

this code work for me

create an empty list where the user input will be stored.

num_list = []

while True:
userNumber = input("Please enter a number. Type 'Done' if you want to stop: ")
if userNumber == 'Done':
break
try:
userNumberint = int(userNumber)
except:
print("Invalid input")
continue

# the user's input will be appended in num_list.    
num_list.append(userNumberint)

added an if statement to check if the list is empty.

if bool(num_list):
# If the list is not empty, it will print the max and min numbers.
print(f"Maximum is {max(num_list)}")
print(f"Minimum is {min(num_list)}")

If the list is empty, it will print the below statement.

else:
print("Please type some numbers.")

@vicxny
Copy link

vicxny commented Jul 14, 2023

Capture
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break;
try:
n = int(num)
if largest is None or n > largest:
largest = n
elif smallest is None or n < smallest:
smallest = n
except:
print("Invalid input")
continue

print("Maximum is", largest)
print("Minimum is", smallest)

#My solution: Indentation is important and remembering when inputting the numbers don't space by accident.
No need to add space after is in the print Function because the input function would add the space before.

@skyweb3
Copy link

skyweb3 commented Sep 13, 2023

What is the role of 'continue' statement in the 'except:' part of the code after the print statement?

@kameerobin
Copy link

largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == "done":
break
num = int(num)
if largest is None or num > largest:
largest = num
elif smallest is None or num < smallest:
smallest = num
except:
print("Invalid input")
continue

print("Maximum is", largest)
print("Minimum is", smallest)

@FardadF4
Copy link

largest = None
smallest = None
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
try:
num = int(num)
except:
print("Invalid input")
continue

if largest is None:
    largest = num
elif num > largest:
    largest = num
if smallest is None:
    smallest = num
elif num < smallest:
    smallest = num

print("Maximum is", largest)
print("Minimum is", smallest)

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