0

I am attempting to create a basic program which requests a numeric value from the user.

If the value is between .5 and 1 the program should print "good".
If the value is between 0 to 0.49 the output states "fair".
If the numeric input the user provides is outside of 0 to 1 it states: "try again".
If the input cannot be converted to a number it states: "Invalid input".

Here is what I have got so far:

val=abs(1)
while True:
    num = raw_input("Enter a number: ")    
    if num == "val" : break
    print 'try again between 0 to 1'
try:
    num = float(num)
except:
    print "Invalid input"

if .5 < num < 1:
        print 'Good'
if 0 < num < .49:
        print 'Fair'      
timgeb
  • 76,762
  • 20
  • 123
  • 145
user3608523
  • 65
  • 1
  • 7
  • 1
    What exactly is your question? – jonrsharpe Jun 27 '14 at 19:35
  • This may be useful to you: [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Kevin Jun 27 '14 at 19:36

1 Answers1

1

There's a couple issues with your code. I've cleaned up your code with regards to what I think what you actually want to do and commented most of the changes. It should be easily adjustable if you have slightly different needs.

val = 1 # abs(1) is the same as 1
while True: # get user input
    num = raw_input("Enter a number: ")
    try: # indented
        num = float(num) # goes to except clause if convertion fails
        if not 0 <= num <= val: # check against val, not "val",moved to try block
            print 'try again between 0 to 1' # indented
        else:
            break # input ok, get out of while loop
    except ValueError: # indented, only excepting ValueErrors, not processor is burning errors
        print "Invalid input"

if .5 <= num <= 1:
    print 'Good'
else: # 0 <= num < 0.5
    print 'Fair'
timgeb
  • 76,762
  • 20
  • 123
  • 145