In: Computer Science
Until now, you have had to leave your team management program running on your computer indefinitely since you did not want to lose the list of players. Finally, you are ready to add the components to your team management program that will allow you to store the player’s information on your computer’s hard drive, thus, allow you to shut down your program without losing your data.
You will need to modify your program to:
Tips: Think about how you are going to write the data to the text file when you create the load function. Your write function needs to read in the data in the same pattern as the load function, otherwise the data will get jumbled.
Sample output:
C:\>python week6.py
Welcome to the Team Manager
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection> 2
Enter new member's name: Nathan
Contact phone number: 505-908-0670
Jersey number: 21
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection> 2
Enter new member's name: Bobby
Contact phone number: 541-241-0670
Jersey number: 44
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection> 5
Filename to save: members.txt
Saving data...
Data saved.
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection> 9
Exiting Program...
C:\>python week6.py
Welcome to the Team Manager
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection> 1
No current members in memory.
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection> 6
Filename to load: members.txt
Loading data...
Data Loaded Successfully.
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection> 1
Name: Bobby
Phone: 541-241-0670
Jersey Number: 44
Name: Nathan
Phone: 505-908-0670
Jersey Number: 21
===========Main Menu===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.
Selection>9
Exiting Program...
If you have any doubts, please give me comment...
def addmember(players, name, phone, jersey_num):
players.append({'name':name, 'phone':phone, 'jersey_num':jersey_num})
return players
def display(players):
if(len(players)==0):
print("No current members in memory.")
else:
for player in players:
print("Name: "+player['name']+"\nPhone: "+player['phone']+"\nJersey Number: "+str(player['jersey_num']))
def removemember(players, name):
players.remove(name)
return players
def editname(players, oldname, newname, phone, jersey_num):
newplayers = []
for player in players:
if player['name'] == oldname:
if(phone==""):
phone = player['phone']
if(jersey_num == ""):
jersey_num = player['jersey_num']
newplayers.append({'name':newname, 'phone':phone, 'jersey_num':jersey_num})
else:
newplayers.append(player)
return newplayers
def load(filename, players):
try:
fp = open(filename)
print("Loading data...")
for line in fp.readlines():
cols = line.strip().split(",")
cols[2] = int(cols[2])
players.append({'name':cols[0], 'phone':cols[1], 'jersey_num':cols[2]})
fp.close()
return players
except FileNotFoundError:
print("Unable to open file")
return []
def save(filename, players):
fp = open(filename, "w+")
print("Saving data...")
for player in players:
print(player['name']+","+player['phone']+","+player['jersey_num'], file=fp)
fp.close()
players = []
while True:
print("Welcome to the Team Manager")
print("===========Main Menu===========")
print("1. Display Team Roster.")
print("2. Add Member")
print("3. Remove Member")
print("4. Edit Member")
print("5. Save Data.")
print("6. Load Data.")
print("9. Exit Program")
# get the choice
ch = int(input('<Selection>'))
# if else block
if ch == 1:
display(players)
elif ch == 2:
name = input("Enter new member's name:")
phone = input("Contact phone number: ")
jersey_num = input("Jersey number: ")
players = addmember(players, name, phone, jersey_num)
elif ch == 3:
name = input("Enter member name to be removed:")
players = removemember(players, name)
elif ch == 4:
oldname = input("Enter the name of the member you want to edit:")
newname = input("Enter the new name of the member:")
phone = input("Enter new phone number(leave empty to unchange):")
jersey_num = input("Enter new phone number(leave empty to unchange):")
players = editname(players, oldname, newname, phone, jersey_num)
elif ch==5:
filename = input("Filename to save: ")
save(filename, players)
print("Data saved.")
elif ch==6:
filename = input("Filename to load: ")
players = load(filename, players)
print("Data Loaded Successfully.")
elif ch == 9:
print("Exiting Program...")
break