【发布时间】:2019-10-19 00:39:59
【问题描述】:
我对编程完全陌生,现在我在一个无法定义“等级”的小程序上苦苦挣扎。谢谢
我试过返回成绩,但它无论如何都行不通
def computegrade(score,grade):
try:
if score >1.0001:
grade= print("invalid score")
elif score < -0.0001:
grade= print("invalid score")
elif score >= 0.9:
grade= print("A")
elif score >= 0.8:
grade= print("B")
elif score >= 0.7:
grade= print("C")
elif score >= 0.6:
grade= print("D")
elif score < 0.6:
grade= print("F")
except:
print("Invalid")
return grade
score=input("Enter your Score(between 0.0-1.0)\n")
grade=computegrade(score,grade)
请解决“等级”定义正确的问题。谢谢 :) 当您输入时0.7,它说:grade=computegrade(score,grade) NameError: 名称“等级”未定义
【问题讨论】:
-
这里的“行不通”是什么意思?错误消息(如果有)(编辑问题以将其显示为正确定义的文本)?
-
函数中设置的变量默认是本地的。它们不存在于该函数之外。
-
如果异常被触发,您不定义等级。您正在尝试将输入中的字符串与整数进行比较,这会引发类型错误
标签: python function if-statement user-defined-functions