![]() |
[split] max and min - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: [split] max and min (/thread-20181.html) |
[split] max and min - Tbakks - Jul-30-2019 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) RE: [split] max and min - ichabod801 - Jul-30-2019 Did you have a question, or are you just hijacking threads to post random code? RE: [split] max and min - cvsae - Jul-30-2019 (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) |