Looping program basic

Question:

I wanted to ask that how can I make my program loop with basic code, (pls don’t recommend cryptic ones) such as if a user types 1 it reruns the program and if they type anything else, the program ends soo please help me in this problem.

Asked By: PythonBasicsDev

||

Answers:

Use a while True loop and break out of it when a certain condition is met:

while True:
    if int(input("Enter a number: ")) == 1: # if user's input is '1'
        break # break out of the infinite loop 
        
    # do repetitive stuff here ... 
Answered By: hhimko

You have two options:

Iteration

Use an infinite while True loop and break out if the user enters 1

while True:
    inp = input("Enter a number: ")
    if inp == '1':
        break
    # some code goes here

Recursion

Create a recursive function which returns if the user enters 1

def get_input():
    inp = input("Enter a number: ")
    if inp == '1':
        return
    # some code goes here
    get_input()

get_input()
Answered By: The Thonnu
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.