Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] max and min
#1
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)
Reply
#2
Did you have a question, or are you just hijacking threads to post random code?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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) 
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020