Python入门这样改真的对吗?
在学习条件语句的时候,老师给学生出了一个题,根据输入的考试分数给出ABCDE的评定,学生们都是这样做的:score = int(input("请输入分数:")) str1 = "" num = 0 if score < 0 or score > 100: print("请输入正确的分数(0——100)") else: if score >= 90: str1 = "A" elif score >= 80: str1 = "B" elif score >= 70: str1 = "C" elif score >= 60: str1 = "D" else: str1 = "E" print(f"分数为:{score}, 等级为:{str1}")
老师看后,给予肯定,没问题,可是还有更加简洁的思路吗?比如有没有更好的算法。
于是老师给出了一个参考答案:score = int(input("请输入分数:")) str1 = "ABCDE" num = 0 if score < 0 or score > 100: print("请输入正确的分数(0——100)") else: num = score // 10 if num < 6: num = 5 print(f"分数为:{score}, 等级为:{str1[9 - num]}")
大家看看老师给出的这个答案有问题吗?