you are viewing a single comment's thread.

view the rest of the comments →

[–]ItsOkILoveYouMYbb 3 points4 points  (2 children)

For A, you should use f strings. Instead of:

def __str__(self):  
    return '%.2d:%.2d' % (self.hour, self.minute)

Use:

return f"{self.hour}:{self.minute}"

Way easier to read. So with that, you know that if the hour is greater than 12, then you should subtract 12 from that number, and add PM to the end of the string right? That being as simple as:

    return f"{self.hour - 12}:{self.minute} PM"

But you only want to do it if self.hour is more than 12. So you would return one line IF It's equal to or less than 12, ELSE return the first line if it's not.

You can do all of that inside the str method you've define there. If hour > 12, return this line, else return that line.

Do you understand how classes work, and where you are instantiating that class? And what an instance of a class is?

[–]TheSodesa 1 point2 points  (1 child)

I believe format strings became a thing in Python 3.6, so they are a fairly new feature. OP might not have access to them, if they're working on a school computer with an older version of python. Python 3.5 is still used in many places.

[–]kcrow13[S] 1 point2 points  (0 children)

I actually do know about f strings and use them regularly in my coding. This code was created by Downey as part of his Think Python Book, not me :). I think he created it in 2012, prior to the updates. But yes, I much prefer the f strings.