Home > Enterprise >  Updating one of several variables depending on user input in python
Updating one of several variables depending on user input in python

Time:01-10

I'm writing my first proper project in python outside of CodeWars katas and problem exercises in my book, and it is designed to calculate the total weekly volume per muscle group of an exercise program.

What I have written is a large dictionary called bodypart where key = exercise name (i.e. bench press) and value = primary muscle group (i.e. chest).

The program then asks users to enter an exercise and number of sets with the following code:

        # offer option to see valid inputs, then get exercise from user
        print('To see a list of possible exercises, enter "check".')
        exercise = input('What is your first exercise of the day? ')
        if exercise == 'check':
            print(bodypart)
            exercise = input('Please enter an exercise from the list. ')
        while exercise not in bodypart:
            exercise = input('Please enter an exercise from the list. ')
        add_to_part = bodypart.get(exercise)
        print('')

        # get the number of sets and check for valid input
        sets = input('How many sets will you do of this exercise? ')
        if not sets.isdigit:
            sets = input('Please enter a valid number.')
        sets = int(sets)

I created a count variable for each major body part, set at 0. What I then do next seems quite long winded and I feel like there must be a more optimal approach to doing so, but I am quite stuck on how to do it. What I do is add the number of sets the relevant counter based on the value in bodypart:

        # add sets to relevant counter
        if add_to_part == 'biceps':
            biceps  = sets
        if add_to_part == 'triceps':
            triceps  = sets
        if add_to_part == 'chest':
            chest  = sets
        if add_to_part == 'shoulders':
            shoulders  = sets
        if add_to_part == 'back':
            back  = sets
        if add_to_part == 'quads':
            quads  = sets
        if add_to_part == 'hams':
            hams  = sets
        if add_to_part == 'glutes':
            glutes  = sets

Is there a way in python that I can update the relevant variable based on the string stored in bodypart as a value, rather than using an if statement for each individual muscle group?

CodePudding user response:

You can use a dictionary to achieve the behavior you want

Here's a small code snippet -

bodypart_sets = {
    'biceps': 0,
    'triceps': 0,
    'chest': 0,
    'shoulders': 0,
    'back': 0,
    'quads': 0,
    'hams': 0,
    'glutes': 0
}
print(list(bodypart_sets.keys()))

add_to_part = 'chest' # dynamic string
if add_to_part in bodypart_sets:
    bodypart_sets[add_to_part]  = 5

print(bodypart_sets['chest'])
print(bodypart_sets)

this prints -

['biceps', 'triceps', 'chest', 'shoulders', 'back', 'quads', 'hams', 'glutes']
5
{'biceps': 0, 'triceps': 0, 'chest': 5, 'shoulders': 0, 'back': 0, 'quads': 0, 'hams': 0, 'glutes': 0}
  •  Tags:  
  • Related