QUESTION:
Write a program to store the data of Players in a binary file (with playername, Age, gender and
Game) using a dictionary object. Create a function that displays the details of only the female
players from the file.
import pickle
import os
def insert_data():
"""Function to write player dictionary records into a binary file."""
with open("[Link]", "ab") as file:
while True:
player = {}
player['playername'] = input("Enter Player Name: ")
player['Age'] = int(input("Enter Age: "))
player['gender'] = input("Enter Gender (Male/Female): ").strip().capitalize()
player['Game'] = input("Enter Game: ")
# Serialize and write the dictionary object to the file
[Link](player, file)
choice = input("Do you want to add more players? (y/n): ").lower()
if choice != 'y':
break
print("Data successfully saved!\n")
def display_female_players():
"""Function to read from the binary file and display only female players."""
if not [Link]("[Link]"):
print("No data file found.")
return
print("\n--- Details of Female Players ---")
print(f"{'Player Name':<20} {'Age':<10} {'Gender':<10} {'Game':<15}")
print("-" * 60)
found = False
with open("[Link]", "rb") as file:
while True:
try:
# Deserialize and load one dictionary object at a time
player = [Link](file)
if player['gender'] == 'Female':
print(f"{player['playername']:<20} {player['Age']:<10} {player['gender']:<10} {player['Ga
found = True
except EOFError:
break # Reached the end of the file
if not found:
print("No female players found in the records.")
print("-" * 60)
# Main execution menu
if __name__ == "__main__":
while True:
print("\n1. Add Player Details")
print("2. Display Female Players Only")
print("3. Exit")
ch = input("Enter your choice (1-3): ")
if ch == '1':
insert_data()
elif ch == '2':
display_female_players()
elif ch == '3':
print("Exiting program.")
break
else:
print("Invalid choice! Please try again.")