Python: error handling with recursive function in error

recursion error python
python recursion
python recursion list
tail recursion python
python recursion append to list
stop recursion error python
exception handling in python
how to stop a recursive function python

This is my third question. I've already been voted off of the island twice now on stack overflow, so I'm feeling a little iffy about posting.

Can someone please tell me what is wrong with the below code.

So I have a reference to a function within an error. That is to say, if an error occurs, then the code re-references the function it is entered it runs again. However, when I run it the second time I find that the function does not return the value if both of the inputs are correct (numeric) inputs.

    #This is a program that is designed to calculate gross pay:

def main():
    payment = input2()
    print('Gross pay: $', format(payment, ',.2f'), sep='')

def input2():
    try:
        #we're first getting the number of hours that the user is working.

        hours = int(input("How manyu hours did you work?: "))

        pay_rate = int(input("Enter your hourly payrate here: "))

        #display the gross pay
        gross_pay = hours * pay_rate
        payment = gross_pay
        #display the gross pay:
    except ValueError:
        print('Error: Nope')
        input2()
        return payment

IMHO this is not a good use of recursion, a simple while loop would do:

def input2():
    while True:
        try:
            #we're first getting the number of hours that the user is working.
            hours = int(input("How manyu hours did you work?: "))
            pay_rate = int(input("Enter your hourly payrate here: "))

            #display the gross pay
            gross_pay = hours * pay_rate
            return gross_pay
        except ValueError:
            print('Error: Nope')

To fix your recursive call would look like:

def input2():
    try:
        #we're first getting the number of hours that the user is working. 
        hours = int(input("How manyu hours did you work?: "))
        pay_rate = int(input("Enter your hourly payrate here: "))

        #display the gross pay
        gross_pay = hours * pay_rate
    except ValueError:
        print('Error: Nope')
        gross_pay = input2()

    return gross_pay

11. Recursion and exceptions, This function runs without producing an error message, but it doesn't do what we Thanks to recursion, the Python code needed to sum the values of a nested  Process Python aborted (core dumped) More specifically, the interpreter hits a recursion error, then tries to back off and handle it. While handling the error, it hits another one. Knowing that it’s already in stack limit recovery mode, the interpreter decides that the stack is unsalvageable and it aborts.

According to your indentation, the function only returns anything in the Except block. If you pass through all the Try stuff without an error, you never get to the 'return payment'.

Hope that helps.

11. Recursion and exceptions, This function runs without producing an error message, but it doesn't do what we Thanks to recursion, the Python code needed to sum the values of a nested  Errors are the bane of a programmer’s existence. You write an awesome piece of code, are ready to execute it and build a powerful machine learning model, and then poof. Python throws up an unexpected error, ending your hope of quick code execution. Every single one of us has faced this issue and

Fixed your code and here you go,

def main():
    payment = input2()
    print('Gross pay: $', format(payment), sep='')

def input2():
    try:
        #we're first getting the number of hours that the user is working.

        hours = int(input("How manyu hours did you work?: "))

        pay_rate = int(input("Enter your hourly payrate here: "))

        #display the gross pay
        gross_pay = hours * pay_rate

        #display the gross pay
        return gross_pay
    except ValueError
        print('Error: Nope')
        return input2()


main()

It show output this on getting correct input,

On getting incorrect input it will show this,

(Tutorial) Exception and Error Handling in Python, To reproduce this error, let's define a function recursion that will be recursive, meaning it will keep calling itself as an infinite loop method call, you  Question: Tag: python,python-3.x,recursion,error-handling I have created a program that uses recursion to solve simple mazes. In the event of a rather complex maze, I

Python, When you execute a recursive function in Python on a large input ( > 10^4), you might encounter a “maximum recursion depth exceeded error”. This is a  Python exceptions are errors that are detected during execution and are not unconditionally fatal: you will soon learn in the tutorial how to handle them in Python programs. An exception object is created when a Python script raises an exception.

Recursion Error While Handling Recursion Error, Recursion Error While Handling Recursion Error. Jun 3, 2019. Here's a Python function. What happens when it gets called? def f(): f(). If you guessed  This is done to avoid a stack overflow. The Python interpreter limits the recursion limit so that infinite recursions are avoided. Handling recursion limit – The “sys” module in Python provides a function called setrecursionlimit() to modify the recursion limit in Python. It takes one parameter, the value of the new recursion limit.

Exception Handling, It is important to understand some of the basics of Python exception handling. Most C API functions also return an error indicator, usually NULL if they are If exc is a tuple, all exception types in the tuple (and recursively in subtuples) are  With statements can be used with try/catch statements to reduce the amount of code you need to write for handling different kinds of errors. With statements call the __Enter__ and __Exit__ functions that are part of a given class.

Comments
  • What are you expecting to be returned if there isn't an error? Think: where are you returning here?
  • You need a return payement statement above except
  • If you can tell me why you have this in a try-except clause maybe I can help you out. I really don't think you need any recursion for this. Is it just to verify that the two inputs are valid numbers?
  • You need to return input2() in your exception block. But I would suggest reevaluating whether you should be using recursion here vs. a simple while loop.
  • I don't think you can safely return payment in the exception, if there is an error with one of the inputs it'll come out None.
  • @Spencer it also loses any return from the recursive call... the proper return should be return input2()
  • @AChampion Good point. Theoretically he could just pop the return outside of the exception (un-indent it) and that would give the "desired" answer, however I don't think this is a very good idea overall.
  • @AChampion - it shouldn't work for all exceptions. Anything other than a value error on the int conversion should propagate up the stack.
  • @Nikhil see comment above - you aren't capturing the return of the recursive call in the exception block.