0

I just finished this code about frequency of each words but my challenge is to modify it to get the name of the winner in all games and the most total coins that he earned.

The file looks like that (I made the titles "Name , game, coins" just to understand):

Name:     Game:          coins:
Fred      Braingame       80
Eddy      Football        20
Lucy      Puzzlgame       50
Mark      Puzzlgame       70
Edy       Football        100
Mark      Football        60
Fred      Puzzlgame       40

Here is the code of frequencies:

f = open("f.txt", "r")
d = dict()
for res in f: 

        res = res.strip()
        res = res.lower()
        lines = res.split() 
        for line in lines:

                if line in d: 
                        d[line] = d[line]+1
                else:

                    d[line] = 1
f.close()
for key in list(d.keys()):
        print("The count of {} is {}".format(key,d[key]))

The output should be like that:

Mark     130  # because he's who earned the most_total coins in all games.


              # 130 is the total of coins : 70 coin + 60 coins
Med
  • 1
  • 4
  • 2
    What have you tried, and what exactly is the problem with it? – jonrsharpe Apr 08 '20 at 20:50
  • @ jonrsharpe The probleme not with this code but with modifications on it to , I don't know how to compare strings and integers at the same time to print the Name of the winner and the total of coins that he earned which is "Mark 130 coins – Med Apr 08 '20 at 20:53
  • 1
    I would recommend running through a structured tutorial, then. See e.g. https://sopython.com/wiki/What_tutorial_should_I_read%3F. – jonrsharpe Apr 08 '20 at 20:54
  • 1
    @ jonrsharpe thanks dude I'll try – Med Apr 08 '20 at 20:55
  • after `lines = res.split()` your name is in `lines[0]` and your money is in `int(lines[2])` - use the first as key for your dict and add the second to it if already in,else create a key with that amount. See f.e. https://stackoverflow.com/a/8381589/7505395 how to operate with dicts. also lookup defaultdict if you want a more optimized approach. if you are not sure what things do, read the documentation or simply `print` them to figure them out – Patrick Artner Apr 08 '20 at 21:09
  • @Patrick Artner thank u bro – Med Apr 08 '20 at 21:17
  • @Patrick Artner Do u have any idea about changing the print out to get just one Name ?? – Med Apr 08 '20 at 21:24

0 Answers0