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)
#6
Based on your feedback, @ibreeden, I’ve rewritten my script from scratch. In my latest iteration, I’ve done a few things. I've:
  • wrapped my algorithm inside a function and did away with the dictionary loop
  • replaced lowercase-ing of the dictionary keys and am now using capitalize()
Here is my script now:

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):
   if WORKOUT_SCHEDULE[day.capitalize()] == ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'):
       return TRAIN
   elif WORKOUT_SCHEDULE[day.capitalize()] == ('Saturday','Sunday'):
       return (REST, CHILL_OUT)
   else:
       return INVALID_DAY
Here is my test script:

import pytest
from workouts1 import (get_workout_motd,
                     CHILL_OUT, INVALID_DAY)


# About parametrize: https://pybit.es/pytest-coding-100-tests.html
@pytest.mark.parametrize("day, expected", [
   ('Monday', 'Go train Chest+biceps'),
   ('monday', 'Go train Chest+biceps'),
   ('Tuesday', 'Go train Back+triceps'),
   ('TuEsdAy', 'Go train Back+triceps'),
   ('Wednesday', 'Go train Core'),
   ('wednesdaY', 'Go train Core'),
   ('Thursday', 'Go train Legs'),
   ('Friday', 'Go train Shoulders'),
   ('Saturday', CHILL_OUT),
   ('Sunday', CHILL_OUT),
   ('sundAy', CHILL_OUT),
   ('nonsense', INVALID_DAY),
   ('monday2', INVALID_DAY),
])
def test_get_workout_valid_case_insensitive_dict_lookups(day, expected):
   assert get_workout_motd(day) == expected
When I run that test script, here is my traceback: https://pastebin.com/kjt6GjjT

I put my traceback inside a pastebin because it is verbose.

As you can see in my traceback, everything fails with a KeyError. I am way off. All I understand is that my WORKOUT_SCHEDULE dictionary splicing is malformed.

What would you people suggest I try next? Any hints?

This is not homework for a course, but I am pretending that it is. So my ask is that you people provide a series of hints and general guidance rather than a full answer.

Guides I’ve leveraged this time:
Reply


Messages In This Thread
RE: Dictionary lookups exercise (PyBite #109) - by Drone4four - Jul-09-2020, 02:56 PM

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