求助大神啊
nam={0: {1: 99, 2: 89, 3: 79}, 1: {1: 98, 2: 88, 3: 78}, 2: {1: 97, 2: 87, 3: 77}}nameno={1: 'fanghao', 2: 'zhou', 3: 'xu'}
for no in nameno.keys():
print("{:<5}:".format(nameno),end="")
sum=0
for subject_no in nam:
sum+=subject_no
print("{}:{:>3}".format(subjects,subject_no),end="")
print("总分:{:>3},平均:{:.2f}".format(sum,sum/float(len(subject_no))))
>>>fanghao:Traceback (most recent call last):
File "<pyshell#3>", line 5, in <module>
sum+=subject_no
TypeError: 'int' object is not subscriptable
出错了这个程序应该怎么改 nam=ast.literal_eval(f.read())
for no in nameno.keys():
print("{:<5}:".format(nameno),end="")
sum=0
for subject_no in nam:
sum+=nam
print("{}:{:>3}".format(subjects,nam),end="")
print("总分:{:>3},平均:{:.2f}".format(sum,sum/float(len(nam)))) 本帖最后由 BngThea 于 2017-10-20 10:42 编辑
没必要将代码写的这么复杂,借助中间变量,可以更好的理清思路
nam={0: {1: 99, 2: 89, 3: 79}, 1: {1: 98, 2: 88, 3: 78}, 2: {1: 97, 2: 87, 3: 77}}
nameno={1: 'fanghao', 2: 'zhou', 3: 'xu'}
for no in nameno.keys():
print("{:<5}:".format(nameno),end="")
sum = 0
temp = nam.get(no-1)
for i in range(1,4):
sum += temp
print("{}:{:>3}".format(i, temp),end="")
print("\t总分:{:>3},\t平均:{:.2f}".format(sum,sum/float(len(nam)))) 看来还有很多东西要学习啊! 本帖最后由 傻眼貓咪 于 2021-9-5 01:03 编辑
result = {'Fanghao': , 'Zhou': , 'Xu': }
for name in result.keys():
arr = [("%d:%3d,")%(n+1, i) for n, i in enumerate(result)]
print("%10s:"%name, *arr, f"總分:{sum(result)},", f"平均:{sum(result)/len(result)}")
Fanghao: 1: 99, 2: 89, 3: 79, 總分:267, 平均:89.0
Zhou: 1: 98, 2: 88, 3: 78, 總分:264, 平均:88.0
Xu: 1: 97, 2: 87, 3: 77, 總分:261, 平均:87.0
页:
[1]