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)
#14
Hello Pythonistas: I’m back!

Thank you for all of your replies.

@ibreeden: Based on your suggestions, by passing different variables into the motd function, I was able to debug. Also, thank you for the tip to use str.format(). I didn’t realize that variables could be placed inside a string dynamically when using {}. Now I know. I incorporated this into my script (first function below).

@ndc85430: Yes, leveraging the unit tests provided by the instructor is important. To test the latest iteration of my script I’m now using both print and the unit tests.

Here was the next iteration of my motd() function which passed the unit test:

def get_workout_motd(day):
   cap_day = day.capitalize()
   # x = WORKOUT_SCHEDULE.get(cap_day)
   if cap_day in ('Monday',):
       return TRAIN.format(WORKOUT_SCHEDULE['Monday'])
   if cap_day in ('Tuesday',):
       return TRAIN.format(WORKOUT_SCHEDULE['Tuesday'])
   if cap_day in ('Wednesday',):
       return TRAIN.format(WORKOUT_SCHEDULE['Wednesday'])
   if cap_day in ('Thursday',):
       return TRAIN.format(WORKOUT_SCHEDULE['Thursday'])
   if cap_day in ('Friday',):
       return TRAIN.format(WORKOUT_SCHEDULE['Friday'])
   if cap_day in ('Saturday','Sunday'):
       return CHILL_OUT
   else:
       return INVALID_DAY
Hooray! I did it! This produces the correct output but it isn’t very elegant or efficient. It’s ultra naive as well. I explored some of the solutions provided by other PyBite contributors and adapted one of them to improve my script. Here is an alternate solution that I came up with:

def get_workout_motd(day):
   cap_day = day.capitalize()
   if cap_day in WORKOUT_SCHEDULE:
       if cap_day in ('Saturday','Sunday'):
           return CHILL_OUT
       else:
           return TRAIN.format(WORKOUT_SCHEDULE[cap_day])
   else:
       return INVALID_DAY
This one is more Pythonic, right? Much better.

@deanhystad: Thank you for your feedback. Sharing the pseudo code is instructive and helpful. But writing the solution to my homework assignment before I came up with a working solution myself is inappropriate for this subforum. You almost ruined the whole purpose of me learning to do it on my own. You should have shared the pseudo code but waited until after I produced my own working solution before sharing your’s. I have other homework assignments that I am working on next. Dear @deanhystad: For future reference, please wait until after I’ve completed my assignments before sharing best practices.
Reply


Messages In This Thread
RE: Dictionary lookups exercise (PyBite #109) - by Drone4four - Jul-23-2020, 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Type conversion and exception handling (PyBite #110) Drone4four 5 36,827 Jul-27-2020, 11:33 AM
Last Post: DeaD_EyE
  Iterating over dictionaries with the namedtuple function (PyBite #108) Drone4four 7 4,878 Jul-15-2020, 04:23 AM
Last Post: ndc85430
  Dictionary based exercise garvind25 2 2,028 Jul-12-2020, 06:53 PM
Last Post: garvind25
  "Slicing and dicing strings" - - PyBite #105 Drone4four 8 4,468 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