In: Computer Science
I need to write a function that counts the number of total wins and losses.
I have a text file called analysis_data.txt with 1000 lines written
Won
Loss
Won
Loss
Won
Won
...
The function need to do the following:
Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file.
**Can't use the break statement
Let me know if you have any doubts or need any modifications.
def function():
file = open("analysis_data.txt", "r") # op[ejing file in read mode]
data = file.read() #reading the file and saving to variable data
won = data.count("Won") # counting the occurences of won
loss = data.count("Loss")# counting the occurences of loss
print("Won", won) #printing won count
print("Loss", loss) #printing loss count
function()
If you have any doubts, leave a comment below before rating. I'll be happy to assist you further.
Do UPVOTE this as I have put a lot of EFFORT in answering this question. It really helps me.