Skip to content Skip to sidebar Skip to footer

How To Find Min And Max In Python Without Using Lists

I am completely lost on how to solve this problem. We are to write a program that asks a user for an amount of integers and then calculates the minimum and maximum from the integer

Solution 1:

Don’t compare to 0 in the loop. Compare to the actual stored values.

Also setting min and max to the first input and then looping one less time will work as expected. You don’t want hard coded initial values here. You want to compare to the users input itself

num_1 = int(input("How many integers would you like to enter?")) #enter an integer greater than or equal to 1print("Please enter", num_1, "integers.")
_min = int(input())
_max = _minfor i inrange(1,num_1):
    number = int(input()) #reads the integers one at a timeif number > _max:
        _max = number
    if number < _min:
        _min = number

Solution 2:

For each number:

if numbers < min:
    min = numbers
if numbers > max:
    max = numbers

Post a Comment for "How To Find Min And Max In Python Without Using Lists"