The while loop never ends and I do not print the result generated in each iteration

0

I'm new to Python and I have to solve this exercise:

  

Develop a program for the check function that calculates and returns the REGISTRATION_TIME_TIME_DATE_LABORATE of a worker in a day starting from the parameters TIME_IN_ENTERED AND TIME_IN_SETTING

     

Suppose that the one that paid per hour worked is 12, elaborate another program that uses the previous one to calculate the time worked of a fortnight for 20 workers and that write a list with TIME_QUINCENAL_LABORATED and its corresponding PAYMENT_OF_MATH

Since I am new to Python and my teacher wants us to do the exercise but has not taught us anything, if not that he wants us to investigate on our own, I could only think of this:

hora_de_llegada=int(input("ingrese horario de entrada:"))   
hora_de_salida=int(input("ingrese horario de salida"))
horas_laboradas= hora_de_salida - hora_de_llegada

print ("las horas que tabajo son :"+str(horas_laboradas))

n=0
m=n+1

while (n!=20):
    hora_de_llegada=int(input("ingrese horario de entrada:"))
    hora_de_salida=int(input("ingrese horario de salida"))
    horas_laboradas= hora_de_salida - hora_de_llegada
print ("las horas que tabajo son :"+str(horas_laboradas))

The problem is that running it only tells me the time worked for the first 2 data inserted, then I keep inserting the data but it no longer returns the time worked and the cycle never ends.

The version of Python that I occupy is 3.5.2

    
asked by lalo hernandez 11.12.2016 в 22:56
source

1 answer

1

There are two problems in the code:

  • The while cycle as it is is a infinite cycle because the control variable n is declared and initialized to 0 before starting it , but its value is never modified afterwards. This causes the exit condition of the n != 20 never to be fulfilled as n always 0 and the cycle will continue to iterate forever and ever until you force the completion of the process. To work properly you must add one to the control variable n in each iteration of the cycle.

  • Only print the first two data because they are the ones you capture before the while , as you have the last print out of the cycle this will only print once when the cycle ends. Since your cycle is infinite, it will never print anything. If the cycle were not infinite, it would only print the result of the last iteration of the cycle. In order for the data to return at each cycle turn, the print must be within while .

To capture 20 pairs of data and for each return the difference would be simply:

n = 0
while (n != 20):
    hora_de_llegada = int(input("ingrese horario de entrada: "))
    hora_de_salida = int(input("ingrese horario de salida: "))
    horas_laboradas= hora_de_salida - hora_de_llegada
    print("Las horas que trabajó son: ", horas_laboradas)
    n = n + 1

However, it is preferable to use a for provided that the number of iterations is known before it, it is simpler and more efficient:

for _ in range(20):
    hora_de_llegada = int(input("ingrese horario de entrada: "))
    hora_de_salida = int(input("ingrese horario de salida: "))
    horas_laboradas= hora_de_salida - hora_de_llegada
    print("Las horas que trabajó son: ", horas_laboradas)
    
answered by 11.12.2016 / 23:24
source