Jul-03-2020, 09:01 AM
I’m learning how to evaluate value-key pairs in a given dictionary.
Here is the exercise from the online course (not for credit):
At the bottom of this forum post I’ve included some additional / supplemental background and more details about the purpose and description of this exercise and what I am trying to achieve.
Here is my first attempt:
I don’t understand that at all.
So I Google ‘ValueError: dictionary update sequence element #0 has length 6; 2 is required’ which turns up this old StackOverflow question with out of date (Python 2) answers which suggest a malformed dictionary, such as using a comma instead of a colon to separate a value from a key when a dictionary is declared. My dictionary is perfectly formed, so I am not sure why I am still getting this particular error.
I gather I might not be calling the
As you people may be able to tell, I am a Python novice so I am open to as many hints and suggestions that you people can provide. Without providing the complete answer, I am looking forward to hearing from other forum members explain how I can refine and improve my script.
The original exercise I am working on here explicitly calls for using functions and returning certain values. The exercise also calls for dictionary value interpolation. I have deliberately skipped these features but only temporarily. For now I am simply trying to implement the general task using print operations. Afterwards I will use proper return operators and work on interpolation for the TRAIN variable and such.
Here is a little bit more about what I am trying to achieve:
Here is the exercise from the online course (not for credit):
Quote:First title case the passed in day argument
(so monday or MONDAY both result in Monday).
If day is not in WORKOUT_SCHEDULE, return INVALID_DAY
If day is in WORKOUT_SCHEDULE retrieve the value (workout)
and return the following:
- weekday, return TRAIN with the workout value interpolated
- weekend day (value 'Rest'), return CHILL_OUT
Examples:
- if day is Monday -> function returns 'Go train Chest+biceps'
- if day is Thursday -> function returns 'Go train Legs'
- if day is Saturday -> function returns 'Chill out!'
- if day is nonsense -> function returns 'Not a valid day'
Trivia: /etc/motd is a file on Unix-like systems that contains
a 'message of the day'
At the bottom of this forum post I’ve included some additional / supplemental background and more details about the purpose and description of this exercise and what I am trying to achieve.
Here is my first attempt:
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' } day = 'Monday' WORKOUT_SCHEDULE_LOWER = dict(k.lower() for k in WORKOUT_SCHEDULE.keys()) if day not in WORKOUT_SCHEDULE_LOWER.keys(): print(INVALID_DAY) elif day['saturday','sunday'] in WORKOUT_SCHEDULE_LOWER: # elif day('saturday','sunday') in WORKOUT_SCHEDULE_LOWER: # Potential alternative to the above line print(REST) print(CHILL_OUT) elif day['monday', 'tuesday', 'wednesday', 'thursday', 'friday'] in WORKOUT_SCHEDULE_LOWER.keys(): print(TRAIN)Here is my current traceback:
Error:$ python basic_workouts2.py
Traceback (most recent call last):
File "basic_workouts2.py", line 16, in <module>
WORKOUT_SCHEDULE_LOWER = dict(k.lower() for k in WORKOUT_SCHEDULE.keys())
ValueError: dictionary update sequence element #0 has length 6; 2 is required
The problem line is #16 where I use my dictionary generator. The official Python docs explains the ValueError:Quote:exception ValueError
Raised when an operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.
I don’t understand that at all.
So I Google ‘ValueError: dictionary update sequence element #0 has length 6; 2 is required’ which turns up this old StackOverflow question with out of date (Python 2) answers which suggest a malformed dictionary, such as using a comma instead of a colon to separate a value from a key when a dictionary is declared. My dictionary is perfectly formed, so I am not sure why I am still getting this particular error.
I gather I might not be calling the
keys()
method/function properly. I found all kinds of tools referred to in other 10 year old Stack Overflow answers to iterate over a dictionaries, however so many answers refer to Python 2 .iteritems()
and .iterkeys()
which are now deprecated in Python 3.As you people may be able to tell, I am a Python novice so I am open to as many hints and suggestions that you people can provide. Without providing the complete answer, I am looking forward to hearing from other forum members explain how I can refine and improve my script.
The original exercise I am working on here explicitly calls for using functions and returning certain values. The exercise also calls for dictionary value interpolation. I have deliberately skipped these features but only temporarily. For now I am simply trying to implement the general task using print operations. Afterwards I will use proper return operators and work on interpolation for the TRAIN variable and such.
Here is a little bit more about what I am trying to achieve:
Quote:In this task you learn how to lookup values from a dictionary or in Python: dict.
You are presented with WORKOUT_SCHEDULE dict (constant) with keys = days and values = workouts (or rest up). Complete get_workout_motd that receives a day string, title case it so the function can receive case insensitive days, look it up in the dict and do two things:
* If the day (key) is not in the dictionary, return INVALID_DAY, we don't want this function to continue.
* If the key is in the dictionary, return CHILL_OUT or TRAIN depending if it's a REST day or not. The latter you will need to string-interpolate using format.
Also check out the docstring and tests. Have fun and keep calm and code in Python!
Update 25th of Nov 2019: previously this Bite required re-raising the KeyError, but as that's already the default behavior of a missing key in a dict, we changed the requirements to return a value instead.