我有一份上学的任务,其中一项任务是显示学生将获得的成绩。成绩是:
答:90%+
B:80% - 89%
C:70% - 79%
D:60% - 69%
E:50% - 59%
这是一些文件,它是逗号分隔的csv文件:
StudentName分数
Harrison 64
杰克68 杰克61Hayley 86
我想知道/得到一些指导,以便我更好地了解如何创建成绩计算器。 我花了很多年时间试图解决这个问题,但却没有希望。 我的代码:
def determine_grade(scores):
if scores >= 90 and <= 100:
return 'A'
elif scores >= 80 and <= 89:
return 'B'
elif scores >= 70 and <= 79:
return 'C'
elif scores >= 60 and <= 69:
return 'D'
elif scores >= 50 and <= 59:
return 'E'
else:
return 'F'
答案 0 :(得分:1)
import bisect
def determine_grade(scores, breakpoints=[50, 60, 70, 80, 90], grades='FEDCBA'):
i = bisect.bisect(breakpoints, scores)
return grades[i]
答案 1 :(得分:1)
你可以使用 pandas 和 numpy 来做到这一点。类似这样:
import pandas as pd
import numpy as np
#Create a DataFrame
d = { # Creating a dict for dataframe
'StudentName':['Harrison','Jake','Jake','Hayley'],
'Score':[64,68,61,86]}
df = pd.DataFrame(d) # converting dict to dataframe
# Keys get converted to column names and values to column values
#get grade by adding a column to the dataframe and apply np.where(), similar to a nested if
df['Grade'] = np.where((df.Score < 60 ),
'F', np.where((df.Score >= 60) & (df.Score <= 69),
'D', np.where((df.Score >= 70) & (df.Score <= 79),
'C', np.where((df.Score >= 80) & (df.Score <= 89),
'B', np.where((df.Score >= 90) & (df.Score <= 100),
'A', 'No Marks')))))
print(df)
result:
StudentName Score Grade
0 Harrison 64 D
1 Jake 68 D
2 Jake 61 D
3 Hayley 86 B
答案 2 :(得分:0)
试试这个:
def determine_grade(scores):
if scores >= 90 and scores <= 100:
return 'A'
elif scores >= 80 and scores <= 89:
return 'B'
elif scores >= 70 and scores <= 79:
return 'C'
elif scores >= 60 and scores <= 69:
return 'D'
elif scores >= 50 and scores <= 59:
return 'E'
else:
return 'F'
如果你必须将得分与数值进行比较,这就是if scores >= 90 and <= 100:
不正确的原因,但经过简短的编辑就可以了。
答案 3 :(得分:0)
对于scores >= 90 and <= 100
,您可以撰写90 <= scores <= 100
我不知道,如果得分是浮点数或整数。如果得分是浮点数,那么你的比较是不够的。
if scores >= 90 and <= 100:
return 'A'
elif scores >= 80 and <= 89:
return 'B'
如果得分为89.99会怎样?
这是我的解决方案。有一个GRADES_PATTERN。如果有什么变化,你不能改变你的功能。
GRADES_PATTERN = {'A':[90, float('inf')], 'B':[80, 90], 'C':[70, 80], 'D':[60, 70], 'E':[50, 60], 'F':[0, 50]}
def check_grade(score, pattern):
for grade, score_range in pattern.iteritems():
if score_range[0] <= score < score_range[1]:
return grade
raise Exception("score is out of pattern range")
print check_grade(89.99, GRADES_PATTERN)
students = {'Harrison':64, 'Jake': 68, 'Hayley':86}
for name, score in students.iteritems():
print("Student {} hat score {} and grade {}".format(name, score, check_grade(score, GRADES_PATTERN)))
答案 4 :(得分:0)
另一种选择......
如果你不想要一个整数,你可以为浮点数改变它。
grade = int(input("What was your score?")) if grade >=90 and grade <=100: print("A*") elif grade >=80 and grade <=89: print("A") elif grade >=70 and grade <=79: print("B") else: print("Unknown grade")
答案 5 :(得分:0)
您可以这样做,注意if
比较的顺序。
def determine_grade(scores):
if scores >= 90:
return 'A'
elif scores >= 80:
return 'B'
elif scores >= 70:
return 'C'
elif scores >= 60:
return 'D'
elif scores >= 50:
return 'E'
else:
return 'F'
答案 6 :(得分:0)
# Score Grade
# if > 1.0 error Bad score
# >= 0.9 A
# >= 0.8 B
# >= 0.7 C
# >= 0.6 D
# < 0.6 F
# ss (string score)
ss = input ("Enter your score: ")
# fs (float score)
try:
fs = float (ss)
except:
print("Error Bad score")
quit()
#print(fs)
if fs >= 1.0:
print("Error Bad Score")
if fs >= 0.9 :
print ("A")
elif fs >= 0.8 :
print ("B")
elif fs >= 0.7 :
print ("C")
elif fs >= 0.6 :
print ("D")
else:
print ("F")
我试图解决这个很棒的网站所引用的问题:https://www.py4e.com/html3/03-conditional
答案 7 :(得分:-2)
def determine_grade(scores):
if scores >= 0 and <= 39:
return 'U'
elif scores >= 40 and <= 49:
return 'D'
elif scores >= 50 and <= 59:
return 'C'
elif scores >= 60 and <= 69:
return 'B'
elif scores >= 70 and <= 79:
return 'A'
else:
return 'No Marks'