Limit between 0 and 100

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Oct 25 19:31:41 EDT 2008


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



More information about the Python-list mailing list