Python Forum
Dictionary lookups exercise (PyBite #109)
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary lookups exercise (PyBite #109)
#9
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 .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_DAY
When 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.
Reply


Messages In This Thread
RE: Dictionary lookups exercise (PyBite #109) - by Drone4four - Jul-16-2020, 12:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Type conversion and exception handling (PyBite #110) Drone4four 5 36,223 Jul-27-2020, 11:33 AM
Last Post: DeaD_EyE
  Iterating over dictionaries with the namedtuple function (PyBite #108) Drone4four 7 4,858 Jul-15-2020, 04:23 AM
Last Post: ndc85430
  Dictionary based exercise garvind25 2 2,014 Jul-12-2020, 06:53 PM
Last Post: garvind25
  "Slicing and dicing strings" - - PyBite #105 Drone4four 8 4,444 Jun-11-2020, 09:28 PM
Last Post: knackwurstbagel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020