计算成绩打印

时间:2013-10-21 03:16:50

标签: python-3.x

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

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 

1 个答案:

答案 0 :(得分:0)

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

例如,你可以添加这样的东西来计算A等级的数量:

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

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

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