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

时间:2015-02-06 02:31:21

标签: python python-3.x dictionary

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

这是我到目前为止所拥有的:

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()

任何帮助非常赞赏。

1 个答案:

答案 0 :(得分:0)

findavg()返回的值不是dicts,它们只是整数,因此它们没有名称数据。由于python变量的工作原理,你也无法打印变量的名称。

我建议让findavg()返回一个字典或列表或其他数据结构。例如,

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