More conditions in a try/except construct python

我是研究僧i 提交于 2019-12-12 07:02:36

问题


I am trying to run a small program with try/except. It should return a mark between A and F for an input value (score) between 0 and 1. When I run it works as it should, i.e.for an input of 0.6 it returns D, for an input of 0.72 it returns C and so on. If I enter a string I get the error printed after the except statement as It should be. But if I type a number higher than 1 (which is out of range (0-1)) I would like to get the same error after the except statement but I get an A. It seems that

score_float >=0.0 and score_float <= 1.0

does not work. How could I solve it? Thanks

Here is the script:

 score = raw_input('Enter score: ')

try:
    score_float = float(score)
    score_float >=0.0 and score_float <= 1.0
    if score_float < 0.6:
        print 'F'
    elif score_float >= 0.6 and score_float < 0.7 :
        print 'D'
    elif score_float >= 0.7 and score_float < 0.8 :
        print 'C'
    elif score_float >= 0.8 and score_float < 0.9 :
        print 'B'
    else:
        print 'A'    
except:
print 'Error, the score has to be a float number between 0.0 and 1.0'       

回答1:


Testing a float value with comparison operators doesn't raise an exception, so your exception handler is never reached. The if statement is simply false and that branch is not selected.

Either raise an exception manually (using raise ValueError('Value out of range') or handle that case explicitly with another print.

The only statement that you need to care about raising an exception is the float() function call. You should really limit your handler to that statement only, and only catch the ValueError exception that is raised when input is given that is not a float. You can combine the range check at that point to only have one print statement:

score = raw_input('Enter score: ')

try:
    score_float = float(score)
    if not (0.0 <= score_float <= 1.0):
        raise ValueError('Number out of range')
except ValueError:
    print 'Error, please enter a valid number between 0.0 and 1.0'
else:
    if score_float < 0.6:
        print 'F'
    elif 0.6 >= score_float < 0.7:
        print 'D'
    elif score_float < 0.8:
        print 'C'
    elif score_float < 0.9:
        print 'B'
    else:
        print 'A'    

I simplified your tests; you don't need to test for a lower bound that earlier if and elif tests have already discounted.

You could use the bisect module to make translating the float value to a letter simpler:

import bisect

names = ['F', 'D', 'C', 'B', 'A']
values = [0.6, 0.7, 0.8, 0.9, float('inf')]
print names[bisect.bisect(values, score_float)]

Because 1.0 is included in the values that map to 'A' for bisection to work correctly, I used infinity to match the last entry.

Demo:

>>> import bisect
>>> names = ['F', 'D', 'C', 'B', 'A']
>>> values = [0.6, 0.7, 0.8, 0.9, float('inf')]
>>> bisect.bisect(values, 0.72)
2
>>> names[bisect.bisect(values, 0.72)]
'C'
>>> bisect.bisect(values, 0.9)
4
>>> names[bisect.bisect(values, 0.9)]
'A'


来源:https://stackoverflow.com/questions/27970607/more-conditions-in-a-try-except-construct-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!