TypeError:/:';str';和';int';用python
我正在做一个项目,寻找学生三次考试的平均成绩 我的工作似乎一切正常,但我收到的一个错误是以下几行:TypeError:/:';str';和';int';用python,python,python-3.x,syntax,Python,Python 3.x,Syntax,我正在做一个项目,寻找学生三次考试的平均成绩 我的工作似乎一切正常,但我收到的一个错误是以下几行: student_avg = ((firstterm) +str (midterm) +str (final)) / 3 该程序是讲师的配套应用程序。我的教授让我做第一学期的期中考试和期末考试,共5门课 工作了一周后,我在这方面仍然面临一些问题。 有人能检查一下我的代码,看看我的错误是什么吗 def determine_score(grade): if 95<= grade <
student_avg = ((firstterm) +str (midterm) +str (final)) / 3
该程序是讲师的配套应用程序。我的教授让我做第一学期的期中考试和期末考试,共5门课
工作了一周后,我在这方面仍然面临一些问题。
有人能检查一下我的代码,看看我的错误是什么吗
def determine_score(grade):
if 95<= grade <=100:
return 'A+'
elif 91<= grade <=96:
return 'A'
elif 89<= grade <=92:
return 'A-'
elif 85<= grade <=90:
return 'B+'
elif 81<= grade <=86:
return 'B'
elif 79<= grade <=82:
return 'B-'
elif 75<= grade <=80:
return 'C+'
elif 72<= grade <=76:
return 'C'
elif 64<= grade <=73:
return 'D'
elif 0<= grade <=65:
return 'F'
else:
return 'invalide score'
def firstTerm():
fst_sub1=int(input("Enter first term marks of the first subject: "))
fst_sub2=int(input("Enter first term marks of the second subject: "))
fst_sub3=int(input("Enter first term marks of the third subject: "))
fst_sub4=int(input("Enter first term marks of the fourth subject: "))
fst_sub5=int(input("Enter first term marks of the fifth subject: "))
firsttermScore = (fst_sub1+fst_sub2+fst_sub3+fst_sub4+fst_sub5)/5
return 'firsttermScore'
def midTerm():
mid_sub1=int(input("Enter mid term marks of the first subject: "))
mid_sub2=int(input("Enter mid term marks of the second subject: "))
mid_sub3=int(input("Enter mid term marks of the third subject: "))
mid_sub4=int(input("Enter mid term marks of the fourth subject: "))
mid_sub5=int(input("Enter mid term marks of the fifth subject: "))
midtermScore = (mid_sub1+mid_sub2+mid_sub3+mid_sub4+mid_sub5)/5
return 'midtermScore'
def final():
fnl_sub1=int(input("Enter final marks of the first subject: "))
fnl_sub2=int(input("Enter final marks of the second subject: "))
fnl_sub3=int(input("Enter final marks of the third subject: "))
fnl_sub4=int(input("Enter final marks of the fourth subject: "))
fnl_sub5=int(input("Enter final marks of the fifth subject: "))
finalScore = (fnl_sub1+fnl_sub2+fnl_sub3+fnl_sub4+fnl_sub5)/5
return 'finalScore'
total = 0
highest = 0
numStudents = int (input("How Many Students are there? "))
while numStudents < 0 or numStudents > 100:
numStudents = int (input("Please enter a number between 0 and 100? "))
for i in range (numStudents):
student_name = (input("Enter Student's Name Please: "))
firstterm = firstTerm()
midterm = midTerm()
final = final()
student_avg = ((firstterm) +str (midterm) +str (final)) / 3
if (highest < student_avg):
highest = student_avg
grade = student_avg
winner = student_name
list_marks = []
list_marks.append([student_name, student_avg,])
print ("exam result: ", list_marks, "grade is: ", determine_score(grade))
print ("The Student with the higgest average is: ", winner, "With the highest average of: ", highest, "gpa is: " + determine_score(grade) )
def确定分数(等级):
如果在最终打印语句中,您需要确保所有内容都是字符串:
print ("The Student with the higgest average is: ", winner, "With the highest average of: ", str(highest), "gpa is: " + determine_score(grade) )
print ("exam result: ", str(list_marks), "grade is: ", determine_score(grade))
此外,函数应返回值,而不是硬编码字符串:
return firsttermScore
...
return midtermScore
...
return finalScore
如果您想获得准确的分数,还需要对双精度(或浮动)进行操作:
firsttermScore = (fst_sub1+fst_sub2+fst_sub3+fst_sub4+fst_sub5)/5.0
finalScore = (fnl_sub1+fnl_sub2+fnl_sub3+fnl_sub4+fnl_sub5)/5.0
midtermScore = (mid_sub1+mid_sub2+mid_sub3+mid_sub4+mid_sub5)/5.0
student_avg = (firstterm + midterm + final) / 3.0
如果除以整数,则输入数据为整数。结果将为整数,您将失去精度
最后一个print语句将失败,因为list_标记是一个列表而不是字符串:
print ("The Student with the higgest average is: ", winner, "With the highest average of: ", str(highest), "gpa is: " + determine_score(grade) )
print ("exam result: ", str(list_marks), "grade is: ", determine_score(grade))
还有最后一个错误:
finalGrade = final()
您需要将变量的名称更改为与函数不同的名称。您的函数执行一些操作(包括一些计算),然后返回字符串。然后调用它们并将字符串除以3。那是不对的。请用代码解释一下好吗?我的问题只有student_avg几乎要花3小时来解决它,但我不能,例如,在final()中,返回字符串“finalScore”,而不是变量finalScore。第一学期和期中也是如此。更改这些函数以返回变量并再次检查。在firstTerm
中,将return'firsttermScore'
替换为return firsttermScore
(丢失引号),与中期
和最终
相同。然后在student\u avg=((第一学期)+str(期中)+str(期末))/3
中丢失str
s.any1请告诉我如何列出所有的学生成绩谢谢你的帮助。student\u avg呢?在student\u avg中,你是在强迫事情成为字符串并使用“+”符号。字符串由“+”号连接,因此您告诉解释器计算“firsttermScoremidtermScorefinalScore”/3,这不是您想要做的。您的第一条语句(print
)是错误的。它应该像问题中那样工作,我已经修好了。在问题中,“highest”是一个数字,而不是字符串。那也会崩溃的。尽管如此,我还是添加了一个不必要的字符串转换。现在第62行final=final()float对象上的问题不可调用