【问题标题】:Calculating average grade using functions and lists in Python 3.6使用 Python 3.6 中的函数和列表计算平均成绩
【发布时间】:2017-03-19 20:32:00
【问题描述】:

我要做的是根据用户输入制作一个姓名和成绩列表,然后显示该列表并在表格中组织该列表,最后计算平均成绩。

所以我希望我的脚本提示用户输入姓名和成绩,然后将它们存储在一个列表中。此提示将一直重复,直到用户输入一个空字符串(在提示输入名称时按回车键)。

我在存储列表时遇到问题,输入空字符串时开始打印和计算。

这是我目前得到的:

    #   Importing Python tabulate package
from tabulate import tabulate

#   Defining functions
def getRec():
    #   Get a record from user input, return an empty of the user entered an empty string as input
    name = str(input("Enter a name: "))
    if name != "":
        score = int(input("Enter the grade of " + name +": "))
        return name,score
    else:
        return None

def addList(List,rec):
    #   Add a rec into the list
    List = tuple(rec)
    return List,rec

def putRec(List):
    #   Print a record in a specific format
    print(List)

def printTable(List):
    #   Print a table with a heading
    print(tabulate(List,showindex="always",headers=["Index","Name","Grade"]))

def average(List):
    #   Computes the average score and return the value
    total = List[0][1]
    avg = total/len(List)
    print("Average Grade: ",avg)

#   Main function

List = []
rec = []
while True:
    rec = list(getRec())
    List = addList(List,rec)

    if rec == None:
        for index in range(len(List)):
            print()
            putRec(List)
            print()
            printTable(List)
            print()
            average(List)
        break

当我尝试启动打印和计算时,出现错误,因为我在第一个函数中返回了“无”。但是如果我返回零,列表就变成了零。我需要帮助来尝试启动我的其他功能,并可能修复我如何根据输入创建列表。

感谢任何帮助。提前致谢。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    在将记录添加到列表之前,您需要检查 getRec() 是否返回 None :

    #   Main function
    
    List = []
    rec = []
    while True:
        result = getRec()
        print(result)
    
        if result == None:
            for index in range(len(List)):
                print()
                putRec(List)
                print()
                printTable(List)
                print()
                average(List)
            break
    
        else:
            rec = list(result)
            addList(List,rec)
    

    您的addList 函数也有错误。您必须使用.append() 方法将项目添加到列表中。那么就不需要返回值了。

    def addList(List,rec):
        #   Add a rec into the list
        List.append(tuple(rec))
    

    通过这 2 项更改,代码应该可以正常运行,但平均成绩仍然是错误的。您需要使用 for 循环来计算所有成绩的总和:

    def average(List):
        #   Computes the average score and return the value
        total = 0
    
        for r in List:
            total += r[1]
    
        avg = total/len(List)
        print("Average Grade: ",avg)
    

    希望对你有帮助:)

    编辑:

    不知道为什么要使用循环来打印整个表格。你只是不需要它。这是没有循环的代码:

    while True:
        result = getRec()
        print(result)
    
        if result == None:
            print()
            putRec(List)
            print()
            printTable(List)
            print()
            average(List)
            break
    
        else:
            rec = list(result)
            addList(List,rec)
    

    【讨论】:

    • 代码有效!非常感谢你的帮助。但是,代码会打印两次列表、表格和平均值。
    • 你是对的!通过使用 for 循环,我最终按列表中元素的数量打印所有内容。感谢您的关注。
    猜你喜欢
    • 2019-08-25
    • 2014-11-03
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    相关资源
    最近更新 更多