Dec-21-2020, 01:51 PM
Hint: Use a function to split user-input and logic to get minimum and maximum.
You get only min and max, all other values are not stored.
def get_number(): """ Helper function to get one int from user input """ while True: num_input = input("Enter a number: ") if num_input.lower() == "done": return None try: return int(num_input) except ValueError: print('Invalid input') def get_min_max(): """ Primitive implementation using a list with min and max. """ numbers = [] while True: number = get_number() if number is None: break numbers.append(number) return min(numbers), max(numbers) def get_min_max_oneshoot(): """ Returns minimum and maximum of entered ints. The ints itself are not stored in a list. """ first_run = True minimum = None maximum = None while True: number = get_number() if number is None: break if first_run: first_run = False minimum = number maximum = number continue minimum = min(number, minimum) maximum = max(number, maximum) return minimum, maximum min_value_1, max_value_1 = get_min_max_oneshoot() # min_value_2, max_value_2 = get_min_max()The function
get_min_max
is the easier implementation.get_min_max_oneshoot
is like your implementation, but without storing any history values.You get only min and max, all other values are not stored.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!