gpt4 book ai didi

python - 将学生数据存储在Python列表中以获取百分比

转载 作者:行者123 更新时间:2023-12-01 00:13:01 26 4
gpt4 key购买 nike

import re
import os
import sys
class Marks:
def __init__(self):
self.marks = []
self.marks_file = '/root/projectpython/mark.txt'
def loadAll(self):
file = open(self.marks_file, 'r')
for line in file.readlines():
name,math,phy,chem = line.strip().split()
name=name
math=int(math)
phy=int(phy)
chem=int(chem)
self.marks=[name,math,phy,chem]
print(self.marks)
file.close()

def percent(self):
dash = '-' * 40
self.loadAll()
for n in self.marks:
print(n)

Book_1 = Marks()
Book_1.percent()

输出:-

['gk', 50, 40, 30]
['rahul', 34, 54, 30]
['rohit', 87, 45, 9]
rohit
87
45
9

但我想以表格格式打印所有值,它只显示最后一条记录。使用列表存储学生数据姓名和分数是否正确?

最佳答案

这里的问题是读取的行

self.marks=[name,math,phy,chem]

每次读取标记时,这都会重新初始化列表

改为使用:

self.marks.append([name,math,phy,chem])

关于python - 将学生数据存储在Python列表中以获取百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59496501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com