简体   繁体   English

我的 python if-else 语句仅返回“else”部分

[英]My python if-else statement only returns the 'else' portion

I am trying to write an if-else statement that calculates the degree class for a given set of marks.我正在尝试编写一个 if-else 语句来计算给定标记集的 class 度数。 Most of my code works fine, but this portion does not.我的大部分代码都可以正常工作,但这部分却不行。 I am expecting to get the result "Lower second class degree' but it prints "Fail" which is the else portion of my statement. I don't get any error messages. I have looked at lots of similar questions on this site and I can't seem to find my error. Thanks for reading, here is my code, which outputs the correct average value but gives 'Fail':我希望得到结果“较低的第二个 class 学位”,但它会打印“失败”,这是我声明的其他部分。我没有收到任何错误消息。我在这个网站上查看了很多类似的问题,我似乎找不到我的错误。感谢阅读,这是我的代码,它输出正确的平均值但给出“失败”:

import numpy as np

level5 = [ 63, 74, 49, 60 ]
level6 = [ 40, 71, 58, 65 ]

# weighted average method

level_five_avg = np.mean([level5])

level_six_avg = np.mean([level6])

weighted_avg = ((0.25 * level_five_avg) + (0.75 * level_six_avg))

if (70 <= weighted_avg <= 100):
    weighted_avg_class = 'First class' 

elif (60 <= weighted_avg <= 69):
    weighted_avg_class = 'Upper second class'
            
elif (50 <= weighted_avg <= 59):
    weighted_avg_class = 'Lower second class'
                
elif (40 <= weighted_avg <= 49):
    weighted_avg_class = 'Third class'
                    
else:
    weighted_avg_class = 'Fail'
    
print('Weighted average method:', weighted_avg_class, '(weighted average =',weighted_avg,')') 

You are ignoring values like 69.5, 59.2, 49.2 etc. So, a better method would be:您忽略了 69.5、59.2、49.2 等值。因此,更好的方法是:

import numpy as np

level5 = [ 63, 74, 49, 60 ]
level6 = [ 40, 71, 58, 65 ]

# weighted average method

level_five_avg = np.mean([level5])

level_six_avg = np.mean([level6])

weighted_avg = ((0.25 * level_five_avg) + (0.75 * level_six_avg))

if (70 <= weighted_avg <= 100):
    weighted_avg_class = 'First class' 

elif (60 <= weighted_avg < 70):
    weighted_avg_class = 'Upper second class'
            
elif (50 <= weighted_avg < 60):
    weighted_avg_class = 'Lower second class'
                
elif (40 <= weighted_avg < 50):
    weighted_avg_class = 'Third class'
                    
else:
    weighted_avg_class = 'Fail'
    
print('Weighted average method:', weighted_avg_class, '(weighted average =',weighted_avg,')') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM