Depending on the seed I put the print of the capabilities does not occur. How to pack?

0
import random
semente=int(input("por favor, digite a semente do gerador aleatório:"))
print("               ")
print("Início da simulação")
random.seed(semente)
cap_inicial_1 = random.randrange(20,51)
cap_inicial_2 = random.randrange(20,51)
cap_inicial_3 = random.randrange(20,51)
while(cap_inicial_1 <= cap_inicial_2 or cap_inicial_2 <= cap_inicial_3 or cap_inicial_1 <= cap_inicial_3):
    if(cap_inicial_1 < cap_inicial_2):
        cap_inicial_2 = random.randrange(20,51)
    if(cap_inicial_2 < cap_inicial_3):
        cap_inicial_3 = random.randrange(20,51)
print("     ")
print("capacidade inicial 1: ",cap_inicial_1,"capacidade inicial 2: ",cap_inicial_2,"capacidade inicial 3:",cap_inicial_3)
    
asked by anonymous 28.03.2015 / 21:02

1 answer

0

By its logic, if all three values are drawn equal (eg 42 , 42 , 42 ) then it will never leave the loop:

  • cap_inicial_1 <= cap_inicial_2 ? Yes: 42 <= 42 . Enter the loop;
  • cap_inicial_1 < cap_inicial_2 ? No: 42 == 42 . It does not change anything;
  • cap_inicial_2 < cap_inicial_3 ? No: 42 == 42 . It does not change anything;
  • Return to the beginning of the loop, without changing any of the values: repeat the same as it occurred, infinitely ...

As the numbers are random, some seeds will give values of this type, others do not. There is nothing to do but rethink your logic. I can not make a suggestion because I do not know what this code should do. But the problem is identified.

P.S. Since the condition of your while uses or instead of and , many other combinations of numbers could lead to this same result. For example, 50 , 20 , 20 ...

    
28.03.2015 / 21:11