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
@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