Jul-16-2020, 12:42 PM
My script is now passing 2 of 13 tests. So I’ve made some progress.
As per @deanhystad’s suggestion, I Googled ‘handle Python KeyError’ which turned up Real Python’s “Python KeyError Exceptions and How to Handle Them”. In that doc, Chad Hansen explains that there are different ways to handle a Python KeyError. The usual and most common solution is the
Based on what I have learned above, here is what I have changed since last time:
Here is what my script looks like now with the above changes:
I can already see part of the problem. My line 19 is malformed. What I am trying to do is return both TRAIN as a string with the key passed in as the placeholder. Or in other words, I’m not sure how to return the string of 'Go train Chest+biceps' when the dictionary key is ‘Monday’ but to also return ‘Go train Shoulders’ when the dictionary value is ‘Friday’. I’ve tried changing line 19 to:
Is there anything else you might suggest @ibreeden to help improve the latest iteration of my script?
What other hints and tips could you both make without giving me the solution?
Thank you both for your help and advice so far. I appreciate your patience with my novice questions.
As per @deanhystad’s suggestion, I Googled ‘handle Python KeyError’ which turned up Real Python’s “Python KeyError Exceptions and How to Handle Them”. In that doc, Chad Hansen explains that there are different ways to handle a Python KeyError. The usual and most common solution is the
.get()
method. So I went with that. w3school’s doc covering the “Python Dictionary get() Method” was helpful too. From these docs, I learned how to retrieve a dictionary’s keys and return their corresponding value pair using .get(). For example with, my_dict.get(keyname, value)
, the keyname
is a required parameter and value
is a substitute which is returned if the keyname doesn’t exist. Based on what I have learned above, here is what I have changed since last time:
- I’m using the .get() method
- I’m capitalizing the day and assigning it to a new variable which I use to call the dictionary value.
- I’ve replaced the three instances of the equality operator with
in
(I got this idea from @ibreeden feedback)
Here is what my script looks like now with the above changes:
INVALID_DAY = 'Not a valid day' REST = 'Rest' CHILL_OUT = 'Chill out!' TRAIN = 'Go train {}' WORKOUT_SCHEDULE = { 'Friday': 'Shoulders', 'Monday': 'Chest+biceps', 'Saturday': 'Rest', 'Sunday': 'Rest', 'Thursday': 'Legs', 'Tuesday': 'Back+triceps', 'Wednesday': 'Core' } def get_workout_motd(day): cap_day = day.capitalize() x = WORKOUT_SCHEDULE.get(cap_day) if x in ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'): return (TRAIN, 'Go train {}') if x in ('Saturday','Sunday'): return CHILL_OUT, REST else: return INVALID_DAYWhen I run the test script,
$ python -m pytest test_workouts.py
, here is my traceback in full. I can already see part of the problem. My line 19 is malformed. What I am trying to do is return both TRAIN as a string with the key passed in as the placeholder. Or in other words, I’m not sure how to return the string of 'Go train Chest+biceps' when the dictionary key is ‘Monday’ but to also return ‘Go train Shoulders’ when the dictionary value is ‘Friday’. I’ve tried changing line 19 to:
return TRAIN, 'Go train {}'
(without the brackets) or to return 'Go train {TRAIN}
. This part of the test is still failing. To improve this particular line, I Googled ‘passing in Python string variables’ which turned up all kinds of recent tutorials describing f-strings which are very basic and not specific to returning strings as part of a function with a dictionary value as a placeholder, which is what I need in my case. Therefore, what further hints or Google search terms might you suggest this time, @deanhystad? Is there anything else you might suggest @ibreeden to help improve the latest iteration of my script?
What other hints and tips could you both make without giving me the solution?
Thank you both for your help and advice so far. I appreciate your patience with my novice questions.