无法在Python中显示键和值(dict)
我目前正在制作一个程序,你必须找到一些孩子测试的平均值,然后将它们与最高平均值一起打印出来 以下是我目前掌握的情况:无法在Python中显示键和值(dict),python,python-3.x,dictionary,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):
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)
我不想使用任何进口产品
代码更新
非常感谢您的帮助。findavg返回的值不是dicts,它们只是整数,因此没有名称数据。由于python变量的工作方式,您也不能打印变量的名称 我建议让findavg返回一个dict、list或其他数据结构。比如说,
chris = {'name': 'chris', 'avg': (test1['chris']+test2['chris']+test3['chris'])/3}
我试过这样做,但不起作用;查看我的代码更新。只需执行chris={'name':'chris','avg':test1['chris']+test2['chris']+test3['chris']/3.0}并在以后格式化这些数字。您在“avg”中存储的是字符串而不是数字。已更新。那么现在我应该像以前一样做%.2f吗?在同一个地方?是的,使用打印%.2f%chris['avg')。很好,它可以工作。那么我该如何打印以下内容:Name获得了平均值的最高值?为什么不使用导入,除非这是一个赋值?在这种情况下,老实说,这是一个任务,所以我不能使用导入。正确@Sethmmorton然而,统计是确定的,我注意到统计可以使我找到平均值的行变得容易得多。但我现在不想回去了。为了确认,是的,这是一个任务@SethMMorton
chris = {'name': 'chris', 'avg': (test1['chris']+test2['chris']+test3['chris'])/3}