-2

希望这将非常简单,我需要帮助的只是计算字母等级,然后将它们与列表一起打印。这是我的代码:

def getScores(): 
    f_obj = open("scores.txt", "r") 
    data = f_obj.readlines()   #read file into a list of record strings 
    f_obj.close() 
    return data 

def displayData(data): 
    print("Name           Avg     Grade")#print headings 
    for record in data: 
        name, ex1, ex2, ex3 = record.split()
        exam1 = float(ex1)
        exam2 = float(ex2)
        exam3 = float(ex3)
        avg = round(exam1 + exam2 + exam3) / 3
        if avg >= 100:
            letterGrade = "A"
        elif avg >= 89:
            letterGrade = "B"
        elif avg >= 79:
            letterGrade = "C"
        elif avg >= 69:
            letterGrade = "D"
        elif avg >= 59:
            letterGrade = "F"

就在上面是我卡住的地方,我不知道如何用某些字母等级进行计数。

        print("%-10s   %5s   %5s" % (name, round(avg, 1), letterGrade))

        print()
        print(

def addStudent(): 
    name = input("Enter student name: ") 
    ex1 = int("Enter Exam 1 grade: ") 
    ex2 = int("Enter Exam 2 grade: ")
    ex3 = int("Enter Exam 3 grade: ")
    return name + "\t" + ex1 + "\t" + ex2 + "\t" + ex3 

def storeData(data): 
    f_obj = open("scores.txt", "w") 
    f_obj.writelines(data) 
    f_obj.close() 

def main(): 

    scoreData = getScores() # read data file into a list 

    while True: 
        print("""                    
          Program Options 
    1.) Display Students. 
    2.) Add a new student:  
    3.) Exit Program         """) 
        option = input("Enter option 1, 2, or 3: ") 

        if option == "1": 
            displayData(scoreData) 
        elif option == "2": 
            scoreData.append(addItem())  # add record string to our list 
        elif option == "3": 
            storeData(scoreData) 
            print("Good bye.") 
            break 
        else: 
            print("Not a valid entry.  Please enter 1, 2, or 3.") 

main()    # start the program 
4

1 回答 1

0

提示:您已经知道如何确定一个等级是 A、B、C 等。所以您需要做的就是同时增加一个计数器。

例如,您可以添加这样的内容来计算 A 级的数量:

if avg >= 100:
    letterGrade = "A"
    numAs += 1

然后为其他年级类型做同样的事情。由于这似乎是家庭作业,我会让你弄清楚如何做这一切。最重要的是,即使 Python 对此并不严格,请考虑声明计数器变量的正确位置。

一旦你完成了这项工作,这是一个“额外的功劳”任务:看看你是否可以只使用一个数组来完成所有这些工作。

于 2013-10-21T03:26:21.167 回答