1

I am working on a homework assignment that a user will input a grade percentage and it will output a letter grade. My issue is that I would like to restrict the user to only the number keys and a decimal point. If the user inputs anything else they will be prompted with an error message and will have a chance to input again. Here is my code that will work without the decimal, but I need the int to be float. Please help! Any feedback will be greatly appreciated!!

    def percentLoop()
    while True:
    a = input('Enter a percent: ')
    try:
        number = int(a)
        if (0< number <= 100):
            return number
        else:
            print ('Enter a percent between 0 and 100.')
    except:
        print ('Please enter a percent between 0 and 100.')

Thanks for looking at what I have.

billabrian6
  • 431
  • 3
  • 10
  • 24
  • 1
    See [this][1] for checking that the input is of a desired type. [1]: http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-in-python – Greg Apr 05 '12 at 04:43

1 Answers1

0

I haven't done python for ages, but is it just a matter of changing number = int(a) to number= float(a) ?

John3136
  • 28,809
  • 4
  • 51
  • 69
  • Unfortunately it is not working in the code above.The link that Greg posted above has gotten me very close.I am now having trouble making it complete the loop. – billabrian6 Apr 05 '12 at 13:49