无法在Python中显示键和值(dict)

2024-07-08 07:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个程序,你必须找到一些孩子的测试平均值,然后打印他们与最高平均值

以下是我目前掌握的情况:

def thetests():
    test1 = {'chris':93, 'john':90}
    test2 = {'chris':95, 'john':95}
    test3 = {'chris':91, 'john':88}
    return test1,test2,test3

def updatetests(test1,test2,test3):
    test1_1 = {'oscar':95, 'kevin':94, 'matthew':93, 'christine':90, 'anna':90}
    test2_1 = {'oscar':94, 'kevin':93, 'matthew':98, 'christine':95, 'anna':93}
    test3_1 = {'oscar':89, 'kevin':95, 'matthew':87, 'christine':91, 'anna':93}
    test1.update(test1_1)
    test2.update(test2_1)
    test3.update(test3_1)
    return test1,test2,test3


def findavg(test1,test2,test3):
    chris = (test1['chris']+test2['chris']+test3['chris'])/3
    john = (test1['john']+test2['john']+test3['john'])/3
    oscar = (test1['oscar']+test2['oscar']+test3['oscar'])/3
    kevin = (test1['kevin']+test2['kevin']+test3['kevin'])/3
    matthew = (test1['matthew']+test2['matthew']+test3['matthew'])/3
    anna = (test1['anna']+test2['anna']+test3['anna'])/3
    christine = (test1['christine']+test2['christine']+test3['christine'])/3

    print("chris,","%.2f" % chris)
    print("john,","%.2f" % john)
    print("oscar,","%.2f" % oscar)
    print("kevin,","%.2f" % kevin)
    print("matthew,","%.2f" % matthew)
    print("anna,","%.2f" % anna)
    print("christine,","%.2f" % christine)
    return chris,john,oscar,kevin,matthew,anna,christine

def maxavg(chris,john,oscar,kevin,matthew,anna,christine):
    value1 = chris
    value2 = john
    value3 = oscar
    value4 = kevin
    value5 = matthew
    value6 = anna
    value7 = christine
    if value1 > value2:
        maximum = value1
    elif value2 > value1:
        maximum = value2
        if value3 > maximum:
            maximum = value3
        elif value4 > maximum:
            maximum = value4
        elif value5 > maximum:
            maximum = value5
        elif value6 > maximum:
            maximum = value6
        else:
            maximum = value7

    print("The highest average is:", maximum)

def main():
    x,y,z = thetests()
    x,y,z = updatetests(x,y,z)
    a,b,c,d,e,f,g = findavg(x,y,z)
    maxavg(a,b,c,d,e,f,g)

main()

运行时,出现以下情况:

chris, 93.00
john, 91.00
oscar, 92.67
kevin, 94.00
matthew, 92.67
anna, 92.00
christine, 92.00
The highest average is: 93.0

它确实给了我最高的平均值,但是我想打印出这个人的名字和号码,像这样:

(name) has the highest average, which is: (percentage)

我不想用进口货

代码更新

def thetests():
    test1 = {'chris':93, 'john':90}
    test2 = {'chris':95, 'john':95}
    test3 = {'chris':91, 'john':88}
    return test1,test2,test3

def updatetests(test1,test2,test3):
    test1_1 = {'oscar':95, 'kevin':94, 'matt':93, 'christine':90, 'anna':90}
    test2_1 = {'oscar':94, 'kevin':93, 'matt':98, 'christine':95, 'anna':93}
    test3_1 = {'oscar':89, 'kevin':95, 'matt':87, 'christine':91, 'anna':93}
    test1.update(test1_1)
    test2.update(test2_1)
    test3.update(test3_1)
    return test1,test2,test3


def findavg(test1,test2,test3):
    chris = {'name': 'chris', 'avg': (test1['chris']+test2['chris']+test3['chris'])/3}
    john  = {'name': 'john', 'avg': (test1['john']+test2['john']+test3['john'])/3}
    oscar = {'name': 'oscar', 'avg': (test1['oscar']+test2['oscar']+test3['oscar'])/3}
    kevin = {'name': 'kevin', 'avg':(test1['kevin']+test2['kevin']+test3['kevin'])/3}
    matt  = {'name': 'matt', 'avg': (test1['matt']+test2['matt']+test3['matt'])/3}
    anna  = {'name': 'anna', 'avg': (test1['anna']+test2['anna']+test3['anna'])/3}
    christine = {'name': 'christine', 'avg': (test1['christine']+test2['christine']+test3['christine'])/3}    
    print("chris,",chris['avg'])
    print("john,",john['avg'])
    print("oscar,",oscar['avg'])
    print("kevin,",kevin['avg'])
    print("matt,",matt['avg'])
    print("anna,",anna['avg'])
    print("christine,", christine['avg'])
    return chris,john,oscar,kevin,matt,anna,christine

def maxavg(chris,john,oscar,kevin,matt,anna,christine):
    value1 = chris['avg']
    value2 = john['avg']
    value3 = oscar['avg']
    value4 = kevin['avg']
    value5 = matt['avg']
    value6 = anna['avg']
    value7 = christine['avg']
    if value1 > value2:
        maximum = value1
    elif value2 > value1:
        maximum = value2
        if value3 > maximum:
            maximum = value3
        elif value4 > maximum:
            maximum = value4
        elif value5 > maximum:
            maximum = value5
        elif value6 > maximum:
            maximum = value6
        else:
            maximum = value7

    print("The maximum value is:", maximum)

def main():
    x,y,z = thetests()
    x,y,z = updatetests(x,y,z)
    a,b,c,d,e,f,g = findavg(x,y,z)
    maxavg(a,b,c,d,e,f,g)

main()

非常感谢您的帮助


Tags: defmattjohnchrisavgprinttest1test2
1条回答
网友
1楼 · 发布于 2024-07-08 07:26:04

findavg()返回的值不是dicts,它们只是整数,因此没有名称数据。由于python变量的工作方式,您也不能打印变量的名称

我建议让findavg()返回dict、list或其他数据结构。例如

chris = {'name': 'chris', 'avg': (test1['chris']+test2['chris']+test3['chris'])/3}

相关问题 更多 >

    热门问题