Python Forum
Search for Player ID and return games played and number of times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search for Player ID and return games played and number of times
#1
Hi team,

New to python and as a gamer, I would like to analyze player data. This would be easy on excel with Vlookup, but I'm having trouble in python.

Here is sample data from two files:

player_game_data.txt
0001 9999 65
0001 8521 5
0002 1234 32
0003 3444 45

Game_id.txt
9999 Fortnite
8521 God of War
1243 Call of Duty
3444 World of Warcraft

Code should function like this...
PlayerID Input: 0001

Print:
Fortnite, 65
God of War, 5


f = open("player_game_data.txt","r")
g = open("game_id.txt", "r")

playerGamesPlayed = input("Please type a player's ID to return what games were played and how many times")
Please help guide me in the right direction.
Reply
#2
Hello, welcome to Python and the forums!
It is advisable that instead of using "open" on its own, you use the "with" statement (so-called context manager). You can read about that and how to handle files in the official docs, as well as numerous other sources available online for free.
You will probably want to loop through the file and read each line as a string. Some string methods will come handy, for example splitting the text. After that you will probably be best off saving the read data in a dictionary, or dictionaries.
If you get stuck feel free to post your attempt and explain what the problem is or what the desire result would be compared to actual one.
Reply
#3
Is the code below a good starting point on how to open and read each line and save each (key-value pair) to a dictionary? Thank you all!

Player_Dic = {}
with open('player_game_data.txt', 'r') as f:
    for line in f:
        (key, val) = line.split()
        Player_Dic[int(key)] = val


Game_Data = {}
with open('game_id.txt', 'r') as a:
        (key, val) = line.split()
        Game_Data[int(key)] = val

# Code that loops throught the file and saves each line as a string


# Below is the user input that will search for the Key Id on the list.
ListenersTimesPlayed = input("Type Listener ID")
ListenersTimesPlayed = int(Artist_Played)
Reply
#4
Will I used a nested dictionary to map the first list element in [1] position to to the element in [0] position on the second list.

player_game_data.txt
0001 9999 65
0001 8521 5
0002 1234 32
0003 3444 45

Game_id.txt
9999 Fortnite
8521 God of War
1243 Call of Duty
3444 World of Warcraft

When I print, the result would give me
Fortnite, 65
God of War, 5
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to compare two cards games? python_beginner99 7 3,599 Nov-30-2021, 06:53 PM
Last Post: deanhystad
  times greater number than the previous rhai 4 3,366 Oct-08-2018, 03:27 AM
Last Post: woooee
  How to used the edge detection in opencv python to return number of edge? Vivian 2 2,655 May-04-2018, 08:31 PM
Last Post: nilamo
  Find how many times a user played an artist and how many times disruptfwd8 1 2,550 May-04-2018, 08:32 AM
Last Post: killerrex
  PLayer vs.PLayer game candylicious 1 3,015 Nov-04-2017, 08:33 PM
Last Post: Lux
  The int that appears an odd number of times. MeeranRizvi 6 6,470 Dec-30-2016, 09:54 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020