-1

Please I think there's something wrong with this logic. My code runs almost perfectly. I want the code to only accept inputs greater than 1 and reject inputs lesser than 1 and also reject "strings" but also prompt them to re-enter their inputs again. it works as I've indicated above, but each time I run input and pass a string for a test it rejects it, and prompts the user again, but if I input a correct input it now displays some error. any help would be appreciated.

def get_cents():
    try:
        cents = float(input("Change owed: "))
        if cents > 0:
            return cents
        elif cents < 0:
            print("invalid input, try again")
            get_cents()
    except ValueError:
        print("invalid input, try again")
        get_cents()
rioV8
  • 18,123
  • 3
  • 18
  • 35
Ayoola
  • 13
  • 2
  • _if I input a correct input it now displays some error_ Are we supposed to guess what the error is? – John Gordon May 06 '22 at 19:58
  • 2
    When using recursion to re-run a process, consider that the original function call will continue when the recursive call completes. – deceze May 06 '22 at 20:00
  • Do the answers to this [question](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) help at all? – quamrana May 06 '22 at 20:00
  • It works for me. Just change your `get_cents()` in the function to `return get_cents()` so your return can be passed up the recursive stack back up to the original caller. That being said, I feel like a loop is cleaner than recursive function. – JNevill May 06 '22 at 20:01

0 Answers0