Python, how to declare a variable between a max and a min?


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

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
asked by Ivan 05.04.2018 в 22:49

3 answers


I guess what you're looking for is that when three values are entered, you get the maximum, the minimum, and the one that remains between the two. I guess you can not use list.sort , sorted , etc:

lado_1 = int(input("Ingrese lado:"))
lado_2 = int(input("Ingrese lado:"))
lado_3 = int(input("Ingrese lado:"))

min, med, max = sorted((lado_1, lado_2, lado_3))

print("Valor máximo: {}\nValor medio: {}\nValor mínimo: {}".format(max, med, min))

If you can only use conditionals, there are several algorithms but one very simple one is the following:

  • Starting we assign the three values to the variables minimo , medio and má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 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 have not really invented anything new, it's neither more nor less than the bubble sort ( bubble sort ) only simplified to the minimum expression. It would be simply:

lado_1 = int(input("Ingrese lado: "))
lado_2 = int(input("Ingrese lado: "))
lado_3 = int(input("Ingrese lado: "))

minimo, medio, maximo = lado_1, lado_2, lado_3

if minimo > medio:
    minimo, medio = medio, minimo

if medio > maximo:
    maximo, medio = medio, maximo

if minimo > medio:
    minimo, medio = medio, minimo

print("\nValor máximo: {}\nValor medio: {}\nValor mínimo: {}".format(maximo,

Enter side: 15
  Enter side: -3
  Enter side: 45


Maximum value: 45
  Average value: 15
  Minimum value: -3

answered by 05.04.2018 / 23:29

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 in case of only 3 numbers .

    Lado1=int(input("ingrese un numero\n"))
    Lado2=int(input("ingrese otro numero\n"))
    Lado3=int(input("ingrese un nuemero\n"))

if(Lado1>Lado2 && Lado1<Lado3 || Lado1<Lado2 && Lado1>Lado2 )
    print("El numero intermedio es " + str(Lado1))
else if(Lado2>Lado1 && Lado2<Lado3 || Lado2<Lado1 && Lado2>Lado3)
     print("El numero intermedio es " + str(Lado2))
else if(Lado3>Lado1 && Lado3<Lado2 || Lado3<a && Lado3>Lado2)
    print("El numero intermedio es " + str(Lado3))
     print("No existe número intermedio")

source link

answered by 05.04.2018 в 23:15

If you want to take the intermediate value between 3 values, a quicker option would be to convert the 3 values in a list, order them and return the one in the middle:

>>> lado1 = 6
>>> lado2 = 8
>>> lado3 = 3
>>> valores = sorted([lado1, lado2, lado3])
>>> valores[1]
answered by 05.04.2018 в 23:28