Home > Net >  How to use a while loop to print every number in a range in Python
How to use a while loop to print every number in a range in Python

Time:12-02

I'm working on a practice problem that says, "Make a function that receives an integer as an argument (step) and that prints the numbers from 0 to 100 (included), but leaving step between each one. Two versions: for loop and a while loop.

I can do it using a foor loop:

def function(x):
    count = 0
    for x in range(0, 100, x):
        print(x)

I can't seem to make it work with a while loop. I've tried this:

def function(x):
    count = 0
    while count <= 100:
        count  = x
        print(count)

so please, help. Thank you!

CodePudding user response:

You need to increment count after printing.

def function(x):
    count = 1
    while count <= 100:
        print(count)
        count  = x
  • Related