Search code examples
python-3.xmaxmin

How to print the max and min number of a Python console input?


Everyone,

the task is as follows: to write a program that prints the max and min number of a Python console input.

For example, we enter (the "5" above is "n" => the count of the numbers to be entered):

5
10
30
504
0
60

The Output should say:

Max number: 504
Min number: 0

Below is the solution - could someone please explain to me the definition of max_number = -sys.maxsize and min_number = sys.maxsize (why are both variables' values actually the opposite to their actual ones) along with the if conditional statement, how should the iterations go so that the output is correct?

import sys

n = int(input())
max_number = -sys.maxsize
min_number = sys.maxsize

for i in range(n):
    current_number = int(input())

    if current_number > max_number:
        max_number = current_number
    if current_number < min_number:
        min_number = current_number

print(f'Max number: {max_number}')
print(f'Min number: {min_number}')

Thanks!


Solution

  • You can do something like this

    n = int(input())
    
    numbers = []
    for i in range(n):
        numbers.append(int(input()))
    
    print(f'Max number: {max(numbers)}')
    print(f'Min number: {min(numbers)}')