close

How to write a while loop that prints from 1 to user_num, increasing by 1 each time?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to write a while loop that prints from 1 to user_num, increasing by 1 each time in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to write a while loop that prints from 1 to user_num, increasing by 1 each time?

  1. How to write a while loop that prints from 1 to user_num, increasing by 1 each time?

    I think you might be looking for range
    # range(start, stop, step) for i in range(1, 5): print(i)

  2. write a while loop that prints from 1 to user_num, increasing by 1 each time

    I think you might be looking for range
    # range(start, stop, step) for i in range(1, 5): print(i)

Method 1


I think you might be looking for range

# range(start, stop, step)
for i in range(1, 5):
    print(i)

but if keeping it as a while loop is more attractive, try

i =  1
while i < 5:
    print(i)
    i += 1

Method 2

Here’s the first method using while loop.

i = 1
user_num = int(input())
while i <= user_num:
    print(i)
    i += 1 # incrementation, if not you will have an infinite loop

Here’s another version using for loop.

user_num = int(input())
for i in range(1, (user_num + 1)):
    print(i)

Input: 4

Output:

1
2
3
4

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read