我写了一个分数计算器,你在里面放一个浮点数,然后根据你的得分得到一个分数。我的问题是,我相信我需要一个浮动(输入。。。但是如果你在盒子里写信,那就成了一个错误。。。你知道吗
def scoreGrade():
"""
Determine the grade from a score
"""
gradeA = "A"
gradeB = "B"
gradeC = "C"
gradeD = "D"
gradeF = "F"
score = float(input("Please write the score you got on the test, 0-10: "))
if score >= 9:
print("You did really good, your grade is:", gradeA, ". Congratulations")
elif score >= 7:
print("Your results are good. They earn you a:", gradeB, ". Better luck next time")
elif score >= 5:
print("Not too bad. You got a:", gradeC)
elif score >= 4:
print("That was close...:", gradeD)
elif score < 4:
print("You need to step up and take the test again:", gradeF)
else:
print("Grow up and write your score between 0 and 10")
如果你写了一些分数在0-10之间的东西,有没有办法去掉浮点数并打印最后一个语句?你知道吗
像这样:
继续询问,直到
float
转换工作,而不引发ValueError
异常。你知道吗你能做到的
我建议使用EAFP方法,分别处理好的和坏的输入。你知道吗
请注意,通常您希望从函数返回,而不是在函数中打印。使用函数输出作为print语句的一部分非常详细,函数不必知道这一点。你知道吗
相关问题 更多 >
编程相关推荐