Limit between 0 and 100

Matimus mccredie at gmail.com
Mon Oct 27 12:55:31 EDT 2008


On Oct 25, 1:42 pm, chemicalcloth... at temple.edu 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()
> 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.

You aren't very far off. You are going to need to use 'if' and '<' or
'>' to check for range though.

As in:

if x > 10:
    print "x is greater than 10"

OR:

if 10 < x < 20:
   print "x is between 10 and 20"

If you describe exactly what it is that you don't understand, people
here will be willing to help you to understand it. However, you will
find that people here are very unwilling to do your homework for you.

Matt



More information about the Python-list mailing list