Andrewgorn
Andrewgorn

Reputation: 77

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

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!

Upvotes: 0

Views: 2412

Answers (1)

Ratery
Ratery

Reputation: 2917

You need to increment count after printing.

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

Upvotes: 1

Related Questions