A question if I define a maximum and a minimum using If, how can I define the number or variable that is in the middle? Thank you very much!
Lado1=int(input("Ingrese lado:"))
Ingrese lado:3
>>> Lado2=int(input("Ingrese lado:"))
Ingrese lado:4
>>> Lado3=int(input("Ingrese lado:"))
Ingrese lado:5
>>> if Lado1>Lado2 and Lado1>lado3:
... A=Lado1
...
>>> if Lado2>Lado1 and Lado2>Lado3:
... A=Lado2
...
>>> if Lado3>Lado1 and Lado3>Lado2:
... A=Lado3
...
>>> A
5
if Lado1<Lado2 and Lado1<Lado3:
... B=Lado1
...
>>> if Lado2<Lado1 and Lado2<Lado3:
... B=Lado2
...
>>> if Lado3<Lado2 and Lado3<Lado1:
... B=Lado3
...
>>> B
3
I suppose that what you are looking for is that by entering three values, obtain the maximum, the minimum and the one that remains between them. I guess you can't use
list.sort
,sorted
, etc:If you can only use conditionals, there are several algorithms but a very simple one is the following:
From the start we assign the three values to the variables
minimo
,medio
andmáximo
as they are entered.We check if the minimum value is greater than the average, if so we exchange them.
We check if the average value is greater than the maximum, if it is, we exchange them.
At this point we already have the maximum value. We only have to check again if the minimum value is greater than the average and if so, we exchange them.
We haven't really invented anything new, it's neither more nor less than bubble sort , just simplified to the bare minimum. It would simply be:
you could try this code, it is an adaptation to a code made in c++, it would help you only if you want to get the intermediate if it is only 3 numbers .
source http://diagramas-de-flujo.blogspot.com/2013/03/valor-intermedio-de-3-numeros-codigo-c.html
If you want to get the intermediate value between 3 values, a faster option would be to convert the 3 values into a list, sort them and return the middle one: