[英]Why am I getting a name error when I defined the variable?
def calculate_letter_grades(test_score):
if test_score >= 90:
return "A"
elif test_score >= 80 and test_score < 90:
return "B"
elif test_score >= 70 and test_score < 80:
return "C"
elif test_score >= 60 and test_score < 70:
return "D"
else:
return "F"
def caluculate_avg(scores):
all_grades = 0
for i in range(len(scores)):
all_grades = all_grades + scores[i]
return all_grades / float(len(scores))
for c in range(6):
scores = []
student_name = input("Please enter the student\'s name: ")
for x in range(6):
test_score = int(input("Please enter test scores: "))
scores.append(test_score)
scores_averaged = calculate_avg(scores)
print("Student: ", students_name)
for z in range(len(scores)):
print("Test Score: ", scores[z], "Test Letter Grade: ", calcualte_letter_grades(scores[z]))
print("Average test score: ", scores_averaged)
我得到的錯誤是:
"NameError: name 'calculate_avg' is not defined"
我不明白為什么,我以為我已經定義了它。
我想你錯過了“計算”這個詞
def caluculate_avg(scores):
改成
def calculate_avg(scores):
function 名稱拼寫錯誤“計算”。
您的 function 名稱是caluculate_avg
但您調用calculate_avg
所以這只是一個錯字問題並更改此行
def caluculate_avg(scores):
對此
def calculate_avg(scores):
因為 function 名稱和呼叫中有錯字。 你在定義
def caluculate_avg(scores):
但你正在使用
scores_averaged = calculate_avg(scores)
將 function 定義更改為
def calculate_avg(scores):
您使用不同的名稱調用了 function, “calculate_avg”而不是“calculate_avg”,請確保在調用時使用相同的 function 名稱
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.