Home > database >  Need to convert this For loop to while
Need to convert this For loop to while

Time:01-19

I am trying to convert for loop to while loop in python and I am not very sure how to do it. Need some help here, thanks! This is what I am working with :

#FOR #aplicacion que imprima tablas de multiplicar desde la tabla que yo quiera hasta la tabla que yo quiera elijiendo desde que multiplicacion mostrar hasta que multiplicacion mostar

desde=int(input("desde tabla quiere saber...?"))
hasta=int(input("hasta que tabla quiere saber...?"))
comenzando=int(input("desde que multiplicacion quiere ver?...?"))
multi=int(input("hasta que multiplicacion quiere ver?...?"))

for i in range(desde,hasta 1):
    for a in range(comenzando,multi 1):
        rta= i*a

CodePudding user response:

This is source

desde=int(input("desde tabla quiere saber...?"))
hasta=int(input("hasta que tabla quiere saber...?"))
comenzando=int(input("desde que multiplicacion quiere ver?...?"))
multi=int(input("hasta que multiplicacion quiere ver?...?"))
x = desde
while x <= hasta:
    a = comenzando
    while a <= multi:
        rta = x*a

        a  = 1
    x  = 1
print(rta)

CodePudding user response:

for is perfect for the loops you have, because you know the start and end and there are a fixed number of loops. while is more appropriate for loops where they can run varying number of loops, e.g. prompting repeatedly until a valid email address is entered.

i = desde
while i <= hasta:

    a = comenzando
    while a <= multi:
        rta = i*a

        a  = 1
    i  = 1

This can be done as one while loop, but there would be calculations to work out the a and i values, using divide and modulo/remainder.

  •  Tags:  
  • Related