I am making a program to keep track of my barbell lifts. everything is working fine except I want the user.appendLifts() function to update the value of whichever key is being updated in liftsDict. I go through the process by starting the program, then i hit 1 to update lift, then 1 again to update my bench, and when i enter my new bench it takes me back to the previous loop but doesnt update the json file
import json
# data storage
from os.path import exists as file_exists
# checks if files exists
class Trainee():
def __init__(self):
# Dictionary of trainee lifts. Lists used to track progress
self.liftsDict = {
'Benchpress weight': [],
'Press weight': [],
'Squat weight': [],
'Deadlift weight': []
}
def appendLift(self, lift, weight):
# Update lifts stored in json file
with open('user.json', 'r') as f:
data = json.load(f)
liftsDictJson = data['liftsDict']
lift = lift.title()
try:
weight = int(weight)
except ValueError:
print(f'{weight} is not a number')
if lift in liftsDictJson:
liftsDictJson[lift].append(weight)
print(f'n{lift} set to {weight}')
with open('user.json', 'w') as f:
json.dump(data, f, indent=4)
def toJSON(self):
# Save instance data to JSON file
with open('user.json', 'w') as f:
json.dump(self.__dict__, f, indent=4)
# Initialize program
user = Trainee()
user.setup()
while True:
message = """Welcome back!
Make a selection by entering a number:
[1]Update working sets
[2]Exit
: """
reply = input(message)
if reply == '1':
message = """Which lifts would you like to update?
[1]Benchpress 3x5
[2]Press 3x5
[3]Squat 3x5
[4]Deadlift 1x5
[5]Back
: """
if reply == '1':
message = input('Enter new working set for benchpress 3x5: ')
user.appendLift('Benchpress', message)
if reply == '2':
break
I am not sure where to even begin, ive watched videos and scowered the web, asked chatGPT but nothing is really coming close
Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.