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)
#15
Hi folks,

Sorry for reviving this post - it was very interesting to read the answers etc. but i am actually very keen to know what way of formatting the strings would you recommend to stick with?
I use f-strings (new to python & thanks to my book) but you in this thread actually use the below e.g:
TRAIN = 'Go train {}'
in connection with
return TRAIN.format(x)
Is it perfectly valid in python3 - i mean is it just something that dates back to python2 or it will be still around along the python3 therefore worth of picking it up as another way of formatting strings?

Btw, the below is my code (no string capitalisation as i actually have had all days in lower case hence lower() in code.)

def get_workout_motd(day):
    """Return a value for the given day or warning message."""

    day = day.lower()
    week_schedule = {'monday': 'chest & biceps', 'tuesday': 'back & shoulders',
                    'wednesday': 'neck & thighs', 'thursday': 'legs',
                    'friday': 'belly', 'saturday': 'rest', 'sunday': 'rest'}
    if day in week_schedule:
        if week_schedule[day] == 'rest':
            return 'Chill out!'
        else:
            return f'The day is {day.title()}, go train {week_schedule[day]}.'
    else:
        return 'Not a valid day!'

# run it :)

day = input("What day is it?: ")
print(get_workout_motd(day))
The code obviously does not strictly adhere to the requirements it is just my take at solving it..
Reply


Messages In This Thread
RE: Dictionary lookups exercise (PyBite #109) - by Intr0spective - Aug-10-2020, 06:41 PM

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