Let’s create an algorithm that calculates the maximum of N numbers in Python, that is, it determines the greater value of the numbers entered from the keyboard. Later in the tutorial we will use Python max() on lists and tuples. It is important to train the mind to find solutions even if there are functions already done!

## Maximum of N numbers in Python – example

The first thing to do is ask how many numbers you want to enter and assign this value to N.

Then we ask for the first number and assign it to the maximum variable.

The condition is set: i <N-1 because the first number has already been entered.

Afterwards, a comparison is made for each number entered: if n is greater than maximum, the value just entered is assigned to maximum.

Finally we display the maximum value.

This is a possible solution for the maximum algorithm:

``````
i = 0
N = int(input('How many numbers do you want to enter?:  '))
n_maximum = int(input('Insert the first number:  '))

while i < N-1:
n=int(input('Insert a number:  '))
if n > n_maximum:
n_maximum = n;
i += 1
print('The maximum value is: ', n_maximum)
``````

We could check the value N, so that if the user enters a value less than or equal to zero, it gives an error message.

``````
i = 0
N = int(input('How many numbers do you want to enter?:  '))

if N>0:
n_maximum = int(input('Insert the first number:  '))
while i < N-1:
n=int(input('Insert a number:  '))
if n > n_maximum:
n_maximum = n;
i += 1
print('The maximum value is: ', n_maximum)
else:
print('You must enter a quantity of at least one!')
``````

We can also use cyclic structures to check that N is positive. This is an example of input control:

``````
i = 0
N = int(input('How many numbers do you want to enter?:  '))
while N <= 0:
N = int(input('How many numbers do you want to enter?:  '))

n_maximum = int(input('Insert the first number:  '))

while i < N-1:
n=int(input('Insert a number:  '))
if n > n_maximum:
n_maximum = n;
i += 1
print('The maximum value is: ', n_maximum)
``````

In the next tutorials we will come back to this topic again, proposing other solutions to the search for the maximum of N numbers in Python.

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot