[Solved] How to use while loop and range in python? [closed]


  1. You do not need to cast the int in your line number 1.

Change this:

x=int(10000) 

to this:

x = 10000
  1. Your code is fine, if the intention is to print the original value inside the loop and the last value i.e. 0 after the iterations, you need to print the current value afterwards.

    print(x)

Hence:

x=10000
while x > 0:
    print (x)
    x -= 100

print(x)

OUTPUT:

10000
9900
9800
9700
9600
.
.
.
300
200
100
0

EDIT:

OP: I need for numbers after 100 to be decrease by 10.

You need a if-else condition to handle the numbers after 100.

Something like:

if x <= 100:
    x -= 10
else:
    x -= 100

Hence:

x=10000
while x > 0:
    print (x)
    if x <= 100:
        x -= 10
    else:
        x -= 100   
print(x)

OUTPUT:

10000
9900
9800
9700
.
.
.
300
200
100
90
.
.
30
20
10
0

2

solved How to use while loop and range in python? [closed]