Python 3.保持一定的字母等级

时间:2017-04-23 20:10:51

标签: python

我需要记录每个获得A,B,C,D和F的学生。该项目的说明要求用户输入LETTER成绩而不是百分比分数。那么我如何跟踪每个字母?这就是我所拥有的:

Grades = open("ClassNameGrades.txt", "w")
count = 0
A=0
B=0
C=0
D=0
F=0

Grades.write=input("Enter name of class - ",)
Grades.write= input("Enter name of faculty - ")
numStudents= int(float(input("Enter the amount of students - ")))

def gradeInput(count,numStudents,A1):
    while count<numStudents:
        studentName=input("Enter name of student - ")
        Grades.write(studentName("\n"))
        studentGrade=input("Enter student letter grade - ", )
        Grades.write(studentGrade("\n"))
        count+=count

2 个答案:

答案 0 :(得分:0)

一个简单的if-else块应该可以正常工作:

studentGrade = input("Enter student letter grade - ").upper()
if studentGrade == "A":
    A += 1
elif studentGrade == "B":
    B += 1
elif studentGrade == "C":
    C += 1
elif studentGrade == "D":
    D += 1
elif studentGrade == "F":
    F += 1
else:
    print("Error")

尝试查看字典以获得更有效的解决方案。

答案 1 :(得分:0)

您可以使用dict作为成绩,即您可以将成绩A,B,C,D,E,F存储为:

grades = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0} 

现在您可以比较用户输入的等级。如果它匹配dict键中的任何等级(A,B,C,D,E,F),则增加该等级的数量。

def gradeInput(count,numStudents,A1):
    while count<numStudents:
        studentName=input("Enter name of student - ")
        Grades.write(studentName("\n"))
        studentGrade=input("Enter student letter grade - ", )
        Grades.write(studentGrade("\n"))
        if studentGrade in grades:
            grades[studentGrade] += 1
        count += count