0

I'm building a console-based bill reminder app as my first project and i'm having some trouble ensuring that the user will enter the proper data when it comes to floats.

Based on the python 2.7 documentation and a few answers here on stackoverflow I've come up with the following solution, that is not working.

billAmnt = raw_input('How much is this bill?')
try:
    float(billAmnt)
    return True
except ValueError:
    return False

This will either return True or False, I need to ask the user again if it's false or continue to the next section of code if it's true.

If there is a good explanation of this elsewhere and you want to point me in the right direction i'd be fine with that as well.

Thanks

Connor
  • 7
  • 5
  • http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Lafexlos Jun 30 '15 at 01:01

1 Answers1

0

Any time when you'd like to repeat something until some condition is satisfied, a while loop is probably a good candidate.

def get_bill_amount():
    while True: # repeat until you hit a break statement or a return
        billAmnt = raw_input('How much is this bill?')
        try:
            amount =  float(billAmnt)
            return amount # exit the loop. 
        except ValueError:
            pass
            #Since I've gotten to the end of the while block, start again from the top!
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37