Depending on the seed I put the print of the capacities does not occur. How to tidy up?

Asked

Viewed 182 times

-1

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)
  • Give an example of seed in which this occurs, please. And when you say the print does not occur, the program simply goes out, or enters a loop infinite?

  • 1

    I suggest [Dit] your question in more detail than this code is trying to do, so we can help you better. In any case, I’ve already identified a potential problem, and posted as an answer.

1 answer

0

By their logic, if all three values are drawn equally (ex.: 42, 42, 42) then you’ll never get out of the loop:

  • cap_inicial_1 <= cap_inicial_2 ? Yes: 42 <= 42. Get in the loop;
  • cap_inicial_1 < cap_inicial_2 ? Not: 42 == 42. Changes nothing;
  • cap_inicial_2 < cap_inicial_3 ? Not: 42 == 42. Changes nothing;
  • Go back to the beginning of the loop, without changing any of the values: repeat the same as happened, infinitely...

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.