0

I am new to coding! I am trying to make a baking timetable based on when I start the mix. Now, the minutes are showing up as '09:0' and '10:75' depending on input. I cannot figure out how to format the output. I appreciate everyone's help!

from datetime import time

start = input ("What time are you mixing, in format of HH:MM \n")
h,m = map (int, start.split(":"))

print(f"I am starting at {h:02d}:{m:02d}")


add_salt = h + 1, m + 30
first_turn = h + 2, m + 0
second_turn = h + 2, m + 30
bake = h + 4, m + 15

print(f"{add_salt[0]}:{add_salt[1]} >>> Add salt")
print(f"{first_turn[0]}:{first_turn[1]} >>> Turn #1")
print(f"{second_turn[0]}:{second_turn[1]} >>> Turn #2")
print(f"{bake[0]}:{bake[1]} >>> Time to bake!")
wakana
  • 25
  • 7
  • are you looking for [How to pad zeroes to a string](https://stackoverflow.com/q/339007/10197418)? – FObersteiner Oct 22 '20 at 19:11
  • Not quite, but thank you for reaching out to a newbie like me! – wakana Oct 22 '20 at 20:43
  • Actually, I like the idea of writing your own clock, seems like good practice ;-) In "real life" you'll want to be using the time/datetime module of course. – FObersteiner Oct 23 '20 at 06:13
  • Thank you, MrFuppes! I am working to become more comfortable with datetime module in general. Have a nice day! – wakana Oct 23 '20 at 12:07

2 Answers2

1

Because you are already using datetime library, you might as well could use datetime.time format, following on this and this.

from datetime import datetime, timedelta

start = datetime.now()

add_salt = start + timedelta(hours = 1, minutes = 30) # h + 1, m + 30
first_turn = start + timedelta(hours = 2) # h + 2, m + 0
second_turn = start + timedelta(hours = 2, minutes = 30) # h + 2, m + 30
bake = start + timedelta(hours = 4, minutes = 15) # h + 4, m + 15

print(f"{add_salt.strftime('%H:%M')} >>> Add salt")
print(f"{first_turn.strftime('%H:%M')} >>> Turn #1")
print(f"{second_turn.strftime('%H:%M')} >>> Turn #2")
print(f"{bake.strftime('%H:%M')} >>> Time to bake!")

# out
# 19:17
# 20:47 >>> Add salt
# 21:17 >>> Turn #1
# 21:47 >>> Turn #2
# 23:32 >>> Time to bake!
Felipe Whitaker
  • 470
  • 3
  • 9
  • I think you should also optimize it by adding the `timedelta` for `first_turn` using `add_salt`, as all your steps are sequential (therefore `first_turn = add_salt + timedelta(minutes = 30)`) – Felipe Whitaker Oct 22 '20 at 19:19
  • Also, check out [datetime's timezone aware](https://stackoverflow.com/questions/4530069/how-do-i-get-a-value-of-datetime-today-in-python-that-is-timezone-aware) – Felipe Whitaker Oct 22 '20 at 19:20
  • Thank you so much for looking at my code! The output looks great based on current time as 'start', but I want to be able to modify through input(). I'll play some more! – wakana Oct 22 '20 at 19:34
  • Use `datetime's ` [`datetime.strptime`](https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime) function then, and you should probably accept the answer. – Felipe Whitaker Oct 22 '20 at 19:38
  • 1
    This is what did thanks to your help, Felipe! Sorry, I couldn't post the code here. See my answer below. Thank you! – wakana Oct 22 '20 at 20:36
0

This is what I did after reading Felipe's answer, and I am happy about it! Honestly, I only understand probably 20% of what is up, but hey, it works! Many, many thanks! Wish you can have my bread!

from datetime import time, datetime, timedelta

start = input ("What time are you mixing, in format of HH:MM \n")
h,m = map (int, start.split(":"))

print(f"I am starting at {h:02d}:{m:02d}")

baseline = datetime.strptime(start, "%H:%M")

add_salt = baseline + timedelta(hours = 1, minutes = 30)
first_turn = add_salt + timedelta(minutes = 30)
second_turn = first_turn + timedelta(minutes = 30)
bake = second_turn + timedelta(hours = 1, minutes = 45)

print(f"{add_salt.strftime('%H:%M')} >>> Add salt")
print(f"{first_turn.strftime('%H:%M')} >>> Turn #1")
print(f"{second_turn.strftime('%H:%M')} >>> Turn #2")
print(f"{bake.strftime('%H:%M')} >>> Time to bake!")
Felipe Whitaker
  • 470
  • 3
  • 9
wakana
  • 25
  • 7
  • What didn't you understand? I believe that you should understand every code you write, and put in the effort to understand what you are reading: that's how we grow. Furthermore, you can link to a profile by using the link format using brackets for [name][link] so it shows like [Felipe's](https://stackoverflow.com/users/14403987/felipe-whitaker) – Felipe Whitaker Oct 22 '20 at 21:40
  • You are 100% right, Felipe! I am trying to understand it, though. Let me play with it, as I am confused with strptime. [wakana](https://stackoverflow.com/users/14481915/wakana?tab=profile) – wakana Oct 22 '20 at 22:18
  • Good call! `datetime.striptime` is a method to convert a `str` of known format into `datetime` object. How it works, you ask? Because all of the [PyPi](https://pypi.org/) projects must be on github, you can just check [strptime's code](https://github.com/python/cpython/blob/master/Lib/_strptime.py) or even [datetime's](https://github.com/python/cpython/blob/master/Lib/datetime.py). – Felipe Whitaker Oct 22 '20 at 22:31
  • There is a whole universe out here! I am checking it out. Thanks again! you are the best! [wakana](https://stackoverflow.com/users/14481915/wakana?tab=profile) – wakana Oct 22 '20 at 23:27
  • @FelipeWhitaker Sorry to bug you again. I cannot seem to change the link to my profile. I do not know how it defaults to activity. I tried profile settings, but no. I can add it like this [wakana](https://stackoverflow.com/users/14481915/wakana?tab=profile), but it doesn't change the link at the end. >>> – wakana Oct 22 '20 at 23:52
  • Why do you want to change the link at the end? The link to the autor of the a comment (or a post, or anything) will always go to their profile. What you can do is put a link inside a comment (or a post, or anything). – Felipe Whitaker Oct 23 '20 at 00:35
  • I thought that was what you were asking me to do earlier, as mine links to activity. 'Furthermore, you can link to a profile by using the link format using brackets for [name][link] so it shows like Felipe's' So, am I doing ok? [wakana](https://stackoverflow.com/users/14481915/wakana?tab=profile) – wakana Oct 23 '20 at 00:48
  • Hahahaha, if you are studying you are doing great. I was telling you to edit your post to include my profile - just that. I learned it is possible to do it like a week ago, and though it was interesting. – Felipe Whitaker Oct 23 '20 at 00:57