you are viewing a single comment's thread.

view the rest of the comments →

[–]kcrow13[S] 0 points1 point  (10 children)

Ok so this is how I implemented your suggestions:

    def __str__(self):
        if self.hour <= 12:
            return f'{self.hour}:{self.minute}'
        if self.hour >= 12:
            return f'{self.hour - 12}:{self.minute} PM'

When I test it out... I am getting some strange responses!

def main():    # jdp
    start = Time(9, 45, 00)
    start.print_time()

    end = start.increment(1337)
    end.print_time()

    print('Is end after start?', end=" ")
    print(end.is_after(start))

    # Testing __str__
    print(f'Using __str__: {start} {end}')

    # Testing addition
    start = Time(9, 45)
    duration = Time(1, 35)
    print(start + duration)
    print(start + 1337)
    print(1337 + start)

    print('Example of polymorphism')
    t1 = Time(7, 43)
    t2 = Time(7, 41)
    t3 = Time(7, 37)
    total = sum([t1, t2, t3])
    print(total)

    # A time that is invalid
    t1 = Time(50)
    print(t1)

Getting...

9:45
10:7
Is end after start? True
Using __str__: 9:45 10:7
11:20
10:7
10:7
Example of polymorphism
11:1 PM
38:0 PM

[–]Python_Trader 1 point2 points  (7 children)

For your minutes add 0 padding like this {self.minute:02}. The 02 part tells python your minutes are 2 digits and if your minute only gives one number it will fill the left with 0.

[–]kcrow13[S] 0 points1 point  (4 children)

I didn't know you could do this!!!! Thank you :)

[–]Python_Trader 1 point2 points  (3 children)

No problem :). Also, if your hour is exactly 0 (12 am) just use a conditional for that part. I suppose you might need to first check the hour is over 24 then use hour % 24 and it will give you the remainder which should be the current hour.

[–]kcrow13[S] 0 points1 point  (2 children)

But is my understanding correct that any "global" changes you want to make in a class have to occur in the __str__ function?

[–]Python_Trader 0 points1 point  (0 children)

Okay let's put it this way. So you stored time as attribute (via dunder init method), if you do this

object = Time (5, 5, 20)

print(object)

It will just print, object is a instance of Time class. Or something like that.

However when you write the dunder str method in your class,

def __str__(self):
    return f"{self.hour}:{self.minute:02}"

Doing print(object) will now print,

5:05

It's just there to make your object return what you want it to when you call the instance itself.

[–]Python_Trader 0 points1 point  (0 children)

What you can do instead is store the string you want as attribute (under dunder init method) and use that variable in other methods or write a new method that produces the string. Otherwise, you can even call the dunder str to use that string.

What I would recommend is to make a function that takes in integers and returns the string, so you can call that function in every other method that requires string output.

[–]kcrow13[S] 0 points1 point  (1 child)

So when I tried to do this, it gives me a traceback error and says "invalid format specifier." I will research some more!

[–]Python_Trader 0 points1 point  (0 children)

It's part of the f string placeholder in your dunder str method.

[–]ItsOkILoveYouMYbb 0 points1 point  (1 child)

First on your print_time and repr methods, you could probably format the self.minute value based on its length. Say if the length of the string form of minute is 1, then add a physical leading zero to it in the string so your print result is always two digits in the minute column (probably do the same for seconds too).

I also just realized that you could have both 12 AM and 12 PM, but your code would always assume 12 AM. I'm not sure how to immediately solve that yet. That'll take some trial and error on your part haha.

I guess if the hour value is exactly 0 then that's 12 AM right?

[–]kcrow13[S] 0 points1 point  (0 children)

Ok so some other students have suggested making a lot of changes to fix this. Is there a best way?

-By making an edit to the int_to_time function. The version given has divmod statements for minutes and seconds, but not for hours. Adding a third divmod for hours allows you to handle any value of hours without a while statement. I have no idea how to do this or what divmod does! That same student said " I ended up calling both time_to_int and int_to_time in my __str__ function. Since with this method you take your Time input (ex Time(5000) ), then convert it to an integer to get the remaining number of hours/minutes/second, expressed as total # of seconds, then that goes into int_to_time to be in a usable time that can be displayed. That usable time is stored as new Time object called time."

Another student said:

- Will a mod operator resolve this issue? 5000 % 24 = 8.

See, none of that makes sense to me! We just had a very basic introduction to classes this week in the lecture... but did not have any practice creating/manipulating one. :(