python - Basic Program Control Flow & Exceptions Try -
this question has answer here:
i attempting create basic program requests numeric value user.
if value between .5 , 1 program should print "good".
if value between 0 0.49 output states "fair".
if numeric input user provides outside of 0 1 states: "try again".
if input cannot converted number states: "invalid input".
here have got far:
val=abs(1) while true: num = raw_input("enter number: ") if num == "val" : break print 'try again between 0 1' try: num = float(num) except: print "invalid input" if .5 < num < 1: print 'good' if 0 < num < .49: print 'fair'
there's couple issues code. i've cleaned code regards think want , commented of changes. should adjustable if have different needs.
val = 1 # abs(1) same 1 while true: # user input num = raw_input("enter number: ") try: # indented num = float(num) # goes except clause if convertion fails if not 0 <= num <= val: # check against val, not "val",moved try block print 'try again between 0 1' # indented else: break # input ok, out of while loop except valueerror: # indented, excepting valueerrors, not processor burning errors print "invalid input" if .5 <= num <= 1: print 'good' else: # 0 <= num < 0.5 print 'fair'
Comments
Post a Comment