这是我的代码示例,我有一个包含以下内容的文件:
Teams Won Loss
arsenal 0 0
liverpool0 0
Manchester0 0
我之前使用data[j][i]
来浏览j = row, i= column
的文件。
我正在尝试在我的代码中实现一个类,如果团队赢了或输了那么描述的属性。我将这些属性链接到列中的位置,例如:
self.lost= data[h][1] where h stands for the hometeam.
然后我不想使用data[h][1] += 1
中的def hemmalag_bortalag
而是想以某种方式使用属性将其更改为ex ---> h.won
class Lag:
def __init__(self,won,lost):
self.won = int(won)
self.lost = int(lost)
def won(self):
self.won +=1
self.won = data[h][1]
def lost(self):
self.lost +=1
lost = data[h][2]
def hemmalag_bortalag(data):
print(" 1. Arsenal \n 2. Man Utd \n 3. Liverpool \n 4. Newcastle")
h(self) = int(input("Hometeam?\n"))
b(self) = int(input("Awayteam?\n"))
while b!= h:
hx = int(input("how many goals did the hometeam do?\n"))
bx = int(input("how many goals did the awayteam do\n"))
if hx > bx:
print("Hometeam won!")
data[h][1] +=1 #------> h.won
data[b][2] +=1 #-------> b.lost
elif hx < bx:
print("Awateam won!")
data[b][2] +=1 #-----> b.won
data[h][2] +=1 #------> h.lost
elif hx == bx:
print("Tie!")
print("List is now updated!")
b=h
def mata_in_resultat():
with open('Premier_League.txt', 'r+') as f:
data = [[line.strip().split()[0]] + [int(value) for value in
line.strip().split()[1:]] for line in f ]
f.seek(0)
hemmalag_bortalag(data)
f.write('\n'.join((' '.join(map(str, line)) for line in data)))
f.truncate()
该代码不适用于h is not defined
和int object has no attribute "won"