Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed How do you loop a program in python 3?

+28 votes
asked Dec 31, 2025 by anonymous
closed Apr 6 by Admin
I would like to be able to have a command line type program with onlinegdb and after one line/ans. it exits the program (onlinegdb does)
Is there a continue function or something in python 3 that I can use? Thank you!
closed with the note: answered

5 Answers

+4 votes
answered Jan 6 by Umair Abdullah (220 points)
you can use the "While" Function or the "range()" Function

while True:

    #Your Code

or:

for {varible} in range(#Number of times you want it to repeat):

    ~Your Code
commented Jan 17 by Daniel Uzo-Ogbonna (110 points)
he is correct
0 votes
answered Feb 24 by Dawood (180 points)
reshown Feb 25 by Admin
if you want to do a certant number of times then use range(), here is an example:

for i in range(5):   # Put the number of times you want inside of () and don't forget the :

    print(This will print 5 times!) # 4 spaces is eccential,You can add 4 spaces by pressing tab but it does that by itself

Now try the code!
0 votes
answered Mar 13 by Dawood (180 points)
reshown Mar 13 by Dawood

To make a simple loop, you will need a range() loop, and to make a forever loop, you will need a while loop.

# Remember that this is a comment and that the compiler will ignore it

for index in range(number of times): # << The colon ( : ) is needed and index can be anything.

    print("For the compiler to tell that this is in the loop and something is not in the loop, we need four spaces or a tab")

print("Now this is not in the loop!")

Now we have you know the range(), Let's see how forever loops are done.

while True: # : needed, and python is case-sensitive so use True and not true or TRUE 

4 spaces or tab is needed in every loop!

To break out of the forever loop, we use:

Break

Now you know!

+1 vote
answered Mar 19 by Deep Dharva (480 points)

In Python, looping is essentially how you tell the computer, "Keep doing this until I say stop" or "Do this for every item in this list." There are two primary ways to handle this: for loops and while loops.


1. The for Loop

The for loop is your "go-to" when you know exactly how many times you want to run a block of code, or when you want to iterate over a collection (like a list, string, or range of numbers).

Iterating over a range

To repeat something a specific number of times, use the range() function.

Python

# This will print numbers 0 through 4
for i in range(5):
    print(f"Iteration number: {i}")

Iterating over a list

Python

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(f"I love eating {fruit}s!")

2. The while Loop

A while loop is used when you want to keep running as long as a specific condition is True. It's great for situations where you don't know ahead of time when the loop will end (like waiting for user input).

Python

count = 1
while count <= 3:
    print(f"Count is {count}")
    count += 1  # Important: increment count so the loop eventually ends!

3. Loop Control: break and continue

Sometimes you need to change the behavior of the loop while it's running:

  • break: Exits the loop entirely, even if the condition is still true.

  • continue: Skips the rest of the current block and jumps back to the start of the next iteration.

Example: Using break

Python

while True:
    name = input("Type 'exit' to stop: ")
    if name == "exit":
        break
    print(f"Hello, {name}!")
0 votes
answered Apr 5 by DAKSH PATHAK (150 points)

#yes there is a continue or pass opt. for example u r making a program and you used if: #condition1 and you do not want the program to not execute the else block you can use pass for eg-

print( "a simple example of pass statement" )

a=10

b=20

c= a+b

if c==30 :

    print(f"the sum is {c}) 

else:

   pass

# for making loop(while one)

count = 1
while count <= 3:
    print(f"Count is {count}")
    count += 1 

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...