Python Forum

Full Version: [split] max and min
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
largest = None
smallest = None
while True:

        nums = input("Enter a number: ")
        if nums == "done":
            break
        try:
            num = int(nums)
        except:
            print("Invalid input")
            continue
        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)
Did you have a question, or are you just hijacking threads to post random code?
(Jul-30-2019, 05:02 PM)Tbakks Wrote: [ -> ]
largest = None
smallest = None
while True:

        nums = input("Enter a number: ")
        if nums == "done":
            break
        try:
            num = int(nums)
        except:
            print("Invalid input")
            continue
        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)

I will try to guess, letsay you type 2 numbers 0, 4 pou get as result Maximum is is 4 and Minimum is is None, to avoid this you need store for example the numbers into list and do the comparisons when the done is *called
largest = None
smallest = None
numsList = []

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

    if num == "done":
        largest = max(numsList)
        smallest = min(numsList)
        break

    try:
        num = int(num)
    except:
        print("Invalid input")
        continue
    else:
        numsList.append(num)

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