pythonloopsfor-loopwhile-loop

Converting a for loop to a while loop ( with a range involved)


Converting this to a while loop:

sum = 0
for i in range (10,0,-1):
    sum = sum +1
    print(i,sum)

I can get:

i=1
while i in range(10,0,-1):
    print(i)
    print(i, end=' ')
    i=i+1

The hard part is the range numbers.

(A for loop is a better solution than a while loop.)


Solution

  • Your guess was too close to get the same result using a while loop.

    Use functions len() and list().

    i=1
    while i in range(10,0,-1):
    print(len(list(range(10,0,-1)))-i+1,i)
    i=i+1