-1

I have this dict:

exercise_name = {
            "sets": 3,
            "reps": 12,
            "weight": 100
}

I know I can get a user input with:

exercise_name = input("Insert Exercise Name: ")

But how can I assign my dictionary directly to the input? Something like:

input("Insert Exercise Name: ") = {
    "sets": 3,
    "reps": 12,
    "weight": 100
}

I'd also like to know how to do it with lists, sets, etc.

  • I am not sure what you are asking – Benjamin Breton May 19 '19 at 11:13
  • @BenjaminBreton I want the user to give an input. Then, I want to create a dictionary and store it in a variable equal to the input. Say, for example, the user types "squat". I want squat = {}. If the user types "bench", I want bench = {}. – António Gonçalves May 19 '19 at 11:16

1 Answers1

2

Don't.

How would you know in your program to address that dictionay anyhow? If the user "customizes" the variables name? This sounds like an xy-problem.

You could store the user input in a dict as well:

sillyDict = { input("Insert name") : { "sets": 3, "reps": 12, "weight": 100}}

but to what purpose - how are you going to use it? You would need to know about the given name or iterate over the whole dict if you want to do anything with it.


It looks as if what you really need are classes and lists:

class Exercise: 
    """Capsules data for one exercise. sets/reps/weigh have defaults."""

    def __init__(self, name, sets = 3, reps = 12, weight = 100):
        """'name'd exercise with sets/reps/weight - using defaults:
        sets = 3, reps = 12, weight = 100"""
        self.name = name
        self.sets = sets
        self.reps = reps
        self.weight = weight

    def __str__(self):
         """Friendly representation of this exercise"""
         return f"{self.name}: {self.sets} sets of {self.reps} reps with {self.weight} kg"

    def __repr__(self):
         return str(self) 

# create a plan            
plan = [ Exercise("Squats"), Exercise("BenchPress",3,5,180), Exercise("Pullups",5,22,10)]

# add one by user input (fragile) - no int-validation
plan.append( Exercise(input("What to do? "), 
                      int(input("Sets: ")), 
                      int(input("Reps: ")), 
                      int(input("Weight: "))))

for exer in plan:
    print(exer)

print(plan)

Output:

What to do? Burpies
Sets: 5
Reps: 15
Weight: 0

Squats: 3 sets of 12 reps with 100 kg       # uses the default values
BenchPress: 3 sets of 5 reps with 180 kg
Pullups: 5 sets of 22 reps with 10 kg
Burpies: 5 sets of 15 reps with 0 kg

[Squats: 3 sets of 12 reps with 100 kg, BenchPress: 3 sets of 5 reps with 180 kg, 
 Pullups: 5 sets of 22 reps with 10 kg, Burpies: 5 sets of 15 reps with 0 kg]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I thought about using classes and lists, but I'm a newbie that's still figuring out OOP. But yeah this seems like the best option. And I guess there's no better way to learn than doing. Thanks! – António Gonçalves May 19 '19 at 11:41