Ive made a program to track my barbell lifts. The code I’ve shown below is opening up a serialized pickle dictionary called users, and if users is empty it prompts the user to create a profile.
import pickle
#used to serialize objects
class Trainee():
def __init__(self, measurement, benchpress, press, squat, deadlift, weight):
#parameters of the user
self.measurement = measurement
self.benchpress = benchpress
self.press = press
self.squat = squat
self.deadlift = deadlift
self.weight = weight
with open('users.pickle', 'rb') as f:
pickled_users = pickle.load(f)
#read in the pickled users list
while True:
if not pickled_users:
#if no account is associated with the user, prompts to create a new profile
greeting = 'Welcome! Lifts Tracker is a program created by Matthew Johnson thatn'
greeting +='lets users track their big four barbell lifts progression.n'
greeting +='These are the bench-press, press, squat, and deadlift.n'
greeting +='To get started, please choose a username: '
username = str(input(greeting))
message = 'nSelect I for imperial or M for metric [I/M]: '
reply1 = str(input(message))
message = 'nEnter your benchpress: '
reply2 = int(input(message))
message = 'nEnter your press: '
reply3 = int(input(message))
message = 'nEnter your squat: '
reply4 = int(input(message))
message = 'nEnter your deadlift: '
reply5 = int(input(message))
message = 'nEnter your weight: '
reply6 = int(input(message))
pickled_users[username] = Trainee(reply1, reply2, reply3, reply4, reply5, reply6)
with open('pickled_users', 'wb') as f:
pickle.dump(pickled_users, f)
print(pickled_users)
break
After I run the code in the terminal everything works and it prints the new users profile in pickled users, confirming that it is infact writing to the file. However when I close the program and open it back up again its all gone. What am I doing wrong?
New contributor
Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.