Comparing 2 values from the same line and looping to the next for all line in file

Question:

main code:

fgcuWins = 0
fgcuLoses = 0
ties = 0
file = open("2022_sport.txt", "r")
for lines in file:
    if char[1] == char[3]:
        ties += 1
        lines += 1
    elif char[1] > char[3]:
        fgcuWins += 1
        lines += 1
    else:
        fgcuLoses += 1
        lines += 1

print(fgcuWins)
print(fgcuLoses)
print(ties)

contents of 2022_sport.txt:

1-2
1-3
4-1
1-3
1-0
0-5
1-0
0-2
3-1
3-0
5-1
3-0
2-1
0-0
3-1
2-0
1-0
3-2
1-1

Was trying to compare int in column 1 to int in column 3 for the number of lines in the file. I cannot figure out how to do so and calculate wins loses and ties

Asked By: Mikehouse627

||

Answers:

Read each line of file, split line on ‘-‘, compare left int to right int and increment applicable win, loss, tie count.

wins = 0
losses = 0
ties = 0

with open('2022_sport.txt', 'r') as f:
    for line in f:
        line = line.strip().split('-')
        if line[0] > line[1]:
            wins += 1
        if line[0] < line[1]:
            losses += 1 
        if line[0] == line[1]:  
            ties += 1

print('wins: ' + str(wins))
print('losses: ' + str(losses))
print('ties: ' + str(ties))  

Output:

wins: 12
losses: 5
ties: 2
Answered By: Captain Caveman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.