While loop being ignored

Asked

Viewed 62 times

0

Hello I would like to get help to a question I haven’t found answer, I have 2 loops while being ignored without any explanation (it doesn’t give error simply it passes to the next), the user must enter a float value of hours within a period of 0 to 24 hours and enter an integer value in days between 1 to 31 however conditions are being ignored (I have already commented the part above this loop however it is still ignored, in addition to what I looked for the condition is correct), the first loop related to power of an appliance is correct and is working but the other 2 not.

 #while (potenciaDoArCondicionado <= 0):  # Impossibilita da potência ser negativa ou 0
                #potenciaDoArCondicionado = int(input("Qual a potência desse Ar-condicionado? "))
                #potenciaDoArCondicionado = (potenciaDoArCondicionado/1000) #Fórmula que faz a conversão de Watts para Quilowatt
            horasDoArCondicionado = 0  # Essa variável guarda quantas horas se utiliza o Ar Condicionado

            while(horasDoArCondicionado<=0 and horasDoArCondicionado>24):
                horasDoArCondicionado = float(input("Quantas horas por dia se utiliza este Ar-condicionado desse setor? "))

            diasDoArCondicionado = 0  # Essa variável guarda quantos dias no mês se utiliza o Ar Condicionado
            while(diasDoArCondicionado<1 and diasDoArCondicionado>31):
                diasDoArCondicionado = int(input("Quantas dias no mês se utiliza o Ar-condicionado desse setor? "))
  • 2

    How can something be "less/equal to zero And greater than 24" at the same time? Same thing for the date, how can a day be "less than a And greater than 31"? Give me an example of value that meets any of these situations you used. Obviously they are conditions that will always be false, so it does not meet the requirement for while to occur.

  • Then it would be right?

  • 1

    "AND" means "E" (as long as it is less than one E greater than thirty-one repeat the question), "OR" means "OR" (as long as it is less than one OR greater than thirty-one repeat the question). Which one correctly represents your intention?

  • I need the user to enter a value greater than 0 in hours (because later I will use this value for a consumption calculation) and that this value does not exceed 24 hours. The same for days, must be longer than 1 day and not exceed 31 days

  • 1

    And which of the two sentences I said represents what you just said? Beware that you stated what you need, but in the loop you are dealing with what "does not need" (ie, repeat the question while the value is unsatisfactory)

  • From what I have learned so far for my two conditions to be met it would be E (and), because the OR (or) only needs one correct condition for it to give True. (I ended up noticing something in this code I ended up putting one of the signals of the wrong bottom condition because it would have to be bigger than 1 and not smaller than 1)

  • 1

    With your last comment in mind, reread your question and especially your code. It makes sense for you to perform a repeat loop while the number is less than 0 and greater than 24?

  • It really does not, I ended up getting confused a little because I was thinking with a logic similar to Digital Circuits (and, or, xor, nor, xnor, Nand not). Thanks for the help and sorry for anything. I’m in my first semester and had my first contact with the schedule now.

Show 3 more comments

1 answer

5

You have two repeat loops with while, that have the structure:

while condition:
    block

Where the code block block will be executed while the condition condition is true. If the condition is never true, the code block will not be executed.

In your case, you set two conditions together with the operator and, but the conditions are exclusive to each other and will never be true at the same time.

while(numero <= 0 and numero > 24):
    ...

Either the number is less than or equal to 0 or it will be greater than 24. The two things it cannot be.

By code, you want the user to be asked for a value while it is not valid, being valid only when it is greater than 0 and less than or equal to 24. For that, the tie should be:

while (numero <= 0 or numero > 24):
    ...

Or

while not (numero > 0 and numero < 24):
    ...

Or, in the most simplified and readable way:

while not (0 < numero <= 24):
    ...

Just fix your two ties based on that.

Browser other questions tagged

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