• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Try Until Success with Loop or Recursion

Python Try Until Success with Loop or Recursion

September 10, 2022 Leave a Comment

To try until success in Python, the easiest way is to use a while loop.

def someFunction():
    return someResult()

while True:
    try:
        result = someFunction()
        break
    except Exception:
        continue

You can also create a recursive function which will call the function if there is an error.

def someFunction():
    try:
        result = someResult()
        return result
    except Exception:
        return someFunction()

Note, if this keeps failing, then after 1,000 tries, you will hit the Python recursion limit and error out of your program.


When working with different functions, sometimes you need to try to perform an action which may or may not be successful everytime.

To try an action until you are successful, the easiest way is with a while loop.

Below shows a simple example of how you can use a while loop and a try/except block to try until success in Python.

def someFunction():
    return someResult()

while True:
    try:
        result = someFunction()
        break
    except Exception:
        continue

One thing to note is that it is good practice to not catch all exceptions in one block but to have separate except blocks. An example of separate except blocks is shown below.

def someFunction():
    return someResult()

while True:
    try:
        result = someFunction()
        break
    except ValueError:
        continue
    except NameError:
        continue

Using Recursion to Try Until Success in Python

You can also create a recursive function which will call the function if there is an error. The idea here is that you will try to perform an action and then if it fails, you call the function in the except block.

Note, if this keeps failing, then after 1,000 tries, you will hit the Python recursion limit and error out of your program.

To change the recursion limit, you can use the Python sys module.

Below shows you a simple recursive function in Python which will try until success.

def someFunction():
    try:
        result = someResult()
        return result
    except Exception:
        return someFunction()

Hopefully this article has been useful for you to learn how to try until success in Python.

Other Articles You'll Also Like:

  • 1.  Python Factorial Recursion – Using Recursive Function to Find Factorials
  • 2.  Python gethostbyname() Function – Get IPv4 Address from Name
  • 3.  Changing Python Turtle Speed with speed() Function
  • 4.  Repeat String with * Operator in Python
  • 5.  Python Remove First Element from List
  • 6.  Creating a Random Color Turtle in Python
  • 7.  How to Split List in Half Using Python
  • 8.  Convert pandas Series to Dictionary in Python
  • 9.  Using Python to Sum Even Numbers in List
  • 10.  Using Python to Count Number of True in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy