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)
#7
I think your problem stated out by incorrectly interpreting the assignment. From the assignment:
Quote:First title case the passed in day argument
(so monday or MONDAY both result in Monday).
You are asked, as the first thing in your program, to title case the passed in day assignment. I do not see where you are doing that. If the day is title cased it will match the case of the keys in the dictionary. If you get an invalid key error, you know the day is invalid, and not just a case error. So start by doing what is asked in the assignment.
day = day.capitalize()
As for a key error, that is common. Anybody who writes a program that uses a dictionary lookup and allows free-form user input will have a way to handle invalid keys. Your solution could be made to work, but it is a terrible solution. Recreating the dictionary keys in sets is brittle and error prone and lengthy. Do you think anyone would program in Python if they had to do things like this to use a dictionary? Of course not. There must be a better way. I wonder how many pages show up if you google "handle Python KeyError".

By the way, the exasperated "There has got to be a better way!" test is what I use to let me know when the approach I am using is most likely wrong and that it is time to close the editor and do some research.
Reply


Messages In This Thread
RE: Dictionary lookups exercise (PyBite #109) - by deanhystad - Jul-09-2020, 04:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Type conversion and exception handling (PyBite #110) Drone4four 5 38,425 Jul-27-2020, 11:33 AM
Last Post: DeaD_EyE
  Iterating over dictionaries with the namedtuple function (PyBite #108) Drone4four 7 4,950 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,569 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