Need to convert this For loop to while

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

Upvotes: 0

Views: 85

Answers (2)

Dile
Dile

Reputation: 528

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)

Upvotes: 2

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

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.

Upvotes: -1

Related Questions