454,516 Members | 1,819 Online
Need help? Post your question and get tips & solutions from a community of 454,516 IT Pros & Developers. It's quick & easy.

# Limit between 0 and 100

 P: n/a Hi. I'm very new to Python, and so this is probably a pretty basic question, but I'm lost. I am looking to limit a float value to a number between 0 and 100 (the input is a percentage). I currently have: integer = int() running = True while running: try: per_period_interest_rate = float(raw_input("Enter per-period interest rate, in percent: ")) break except ValueError: print "Please re-enter the per-period interest rate as a number between 0 and 100." I also have to make sure it is a number and not letters or anything. Thanks for the help. James P.S. I don't understand a lot of what I have there, I got most of it from the beginning tutorials and help sections. I have never programmed before, but this is for a school assignment. Oct 25 '08 #1
4 Replies

 P: n/a On Sat, 25 Oct 2008 13:42:08 -0700, chemicalclothing wrote: Hi. I'm very new to Python, and so this is probably a pretty basic question, but I'm lost. I am looking to limit a float value to a number between 0 and 100 (the input is a percentage). I currently have: integer = int() What's this supposed to do? I think writing it as ``integer = 0`` is a bit simpler and more clear. running = True while running: try: per_period_interest_rate = float(raw_input("Enter per-period interest rate, in percent: ")) break except ValueError: print "Please re-enter the per-period interest rate as a number between 0 and 100." You have to check for the range before you leave the loop. The `ValueError` handling just makes sure that the input is a valid float. The ``try``/``except`` structure can have an ``else`` branch. Maybe that can be of use here. Ciao, Marc 'BlackJack' Rintsch Oct 25 '08 #2