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!")
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!
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!")
Related
from datetime import datetime, timedelta
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
How can I find the number of 15 minutes intervals exist between start and finish?
from datetime import datetime, timedelta
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
elapsed = finish - start
number_of_intervals = elapsed / timedelta(minutes=15)
elapsed is the timedelta between start and finish. Divide by 15 minutes to calculate how many 15 minute intervals fit in there.
Note that this returns a float, so includes fractional intervals. Round as appropriate.
You need to find the difference between start and finish in minutes, divide by 15, and make that an int:
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
difference = (finish - start).total_seconds()/60
quarters = int(difference/15)
i would write something similar to this:
from datetime import datetime, timedelta
DATE_TIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
from_date_time = datetime.strptime('2016-06-06T05:00:00.000Z',
DATE_TIME_STRING_FORMAT)
to_date_time = datetime.strptime('2016-06-06T06:00:00.000Z',
DATE_TIME_STRING_FORMAT)
date_times = [from_date_time.strftime(DATE_TIME_STRING_FORMAT)]
date_time = from_date_time
while date_time < to_date_time:
date_time += timedelta(minutes=15)
date_times.append(date_time.strftime(DATE_TIME_STRING_FORMAT))
print(date_times)
Output:
['2016-06-06T05:00:00.000000Z', '2016-06-06T05:15:00.000000Z', '2016-06-06T05:30:00.000000Z', '2016-06-06T05:45:00.000000Z', '2016-06-06T06:00:00.000000Z']
Edit:
If you are interested in just the number of 15 minute intervals you can use something like:
from datetime import datetime, timedelta
DATE_TIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
from_date_time = datetime.strptime('2016-06-06T05:00:00.000Z',
DATE_TIME_STRING_FORMAT)
to_date_time = datetime.strptime('2016-06-06T06:00:00.000Z',
DATE_TIME_STRING_FORMAT)
print((to_date_time-from_date_time) / timedelta(minutes=15))
You can use time library instead of date time. time works with seconds and you should convert minutes to seconds:
import time
interval = 45*60
start = time.time()
finish = time.time() + interval
diff = finish - start
print(diff // (15*60))
Simply compare start and finish like so:
from datetime import datetime, timedelta
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
elapsed = finish - start # This is a timedelta object
reference_interval = 15*60 # interval in seconds
number_of_intervals = elapsed.seconds/reference_interval
As pointed out by Sören, this will not work if 'elapsed' is more than one day, in which case, simply compute the number as follow:
number_of_intervals = (elapsed.days*86400+elapsed.seconds)/reference_interval
# (86400 seconds in a day)
I've been trying to make some sort of simulation of the solar system, and I came across Kepler's equation in order to give me the position of the planet at any given time. I used the Newton-Raphson method in order to calculate the eccentric anomaly. However, I can't seem to make the planet actually orbit properly, and I am not sure what is wrong. The planet simply goes back and forth in a very tiny spot.
I appreciate the help in advance.
from datetime import datetime, timedelta
from math import *
from vpython import *
scene = canvas(title='Solar system simulation',
width=1024, height=720,
center=vector(0,0,0), background = color.black)
sun = sphere(pos = vector(0,0,0), radius = 5, color = color.yellow)
earth = sphere(pos=vector(1,1,0), radius = 1, color = color.blue, make_trail = True)
earth.trail_color = color.white
d1 = datetime.now()
while True:
d1 = d1 + timedelta(minutes = 1)
d2 = datetime(2000, 1, 1, 00, 00)
SecondsFrom2000 = (d1 - d2).total_seconds() #seconds between today and 01.01.2000 at midnight
CenturiesFrom2000 = SecondsFrom2000/(60*60*24*365.25*100) #centuries between today and 2000
e0Earth = 0.01671123 #eccentricity
edotEarth = -0.00004392
eEarth = e0Earth + edotEarth*CenturiesFrom2000
a0Earth = 1.00000018 #semi-major axis[au], from 3000 BC - 3000 AD
adotEarth = -0.00000003 #[au/century]
aEarth = a0Earth + adotEarth*CenturiesFrom2000
L0Earth = 100.46691572 #mean longitude [deg]
LdotEarth = 35999.37306329 #[deg/century]
LEarth = (L0Earth + LdotEarth*CenturiesFrom2000)*(pi/180) #[rad/century]
Pi0Earth = 102.93005885 #longitude of the perihelion [deg]
PidotEarth = 0.31795260 #[deg/century]
PiEarth = (Pi0Earth + PidotEarth*CenturiesFrom2000)*(pi/180)
W0Earth = -5.11260389 #longitude of the ascending node [deg]
WdotEarth = -0.24123856
WEarth = (W0Earth + WdotEarth*CenturiesFrom2000)*(pi/180)
I0Earth = -0.00054346 #inclination [deg]
IdotEarth = -0.01337178
IEarth = (I0Earth + IdotEarth*CenturiesFrom2000)*(pi/180)
MEarth = LEarth - PiEarth #mean anomaly
wEarth = PiEarth - WEarth #argument of perihelion
E0 = 0.1
E1 = MEarth #E = eccentric anomaly
error = 1
while error > 0.000001:
E2 = E1 - (E1 - eEarth*sin(E1)/(1 - eEarth*cos(E1)))
E1 = E2
erreur = abs(E2 - E1)
E_Earth = E2
PEarth = aEarth * (cos(E_Earth) - eEarth) #[au]
QEarth = aEarth * sin(E_Earth)*sqrt(1 - eEarth*eEarth) #[au]
xEarth = cos(wEarth)*PEarth - sin(wEarth)*QEarth
yEarth = sin(wEarth)*PEarth + cos(wEarth)*QEarth
zEarth = sin(IEarth)*xEarth
xEarth = cos(IEarth)*xEarth
xtempEarth = xEarth
xEarth = cos(WEarth)*xtempEarth - sin(WEarth)*yEarth
yEarth = sin(WEarth)*xtempEarth + cos(WEarth)*yEarth
earth.pos = vector(xEarth*10,yEarth*10,zEarth*10)
Your program is too complicated for me to follow, but I would recommend inserting some print statements in the hope that they would uncover where your code is doing something different than want you wanted.
time_1 = datetime.timedelta(hours = hours_1, minutes = minutes_1, seconds = seconds_1)
time_2 = datetime.timedelta(hours = hours_2, minutes = minutes_2, seconds = seconds_2)
when added it gives the answer in days and hours but I really just want the total number of hours.
This should help.
import datetime
time_1 = datetime.timedelta(hours = 10, minutes = 2, seconds = 5)
time_2 = datetime.timedelta(hours = 30, minutes = 5, seconds = 7)
print(time_1 + time_2)
print(((time_1 + time_2).total_seconds()/60)/60)
Output:
1 day, 16:07:12
40.12
I'm just beginning to learn Python on my own, and I'm trying to write a code that will calculate the end time of a jog.
My code so far looks as follows:
def elapsed(t):
t = raw_input('Enter time (hh:mm:ss): ')
th, tm, ts = t.split(':')
return int(ts) + int(tm) * 60 + int(th) * 3600
def mile(m):
m = raw_input('How many miles? ')
return int(m)
start = elapsed('start')
warmup = elapsed('warmup')
wmile = mile('wmile')
tempo = elapsed('tempo')
tmile = mile('tmile')
cooloff = elapsed('cooloff')
cmile = mile('cmile')
hour = (start + warmup * wmile + tempo * tmile + cooloff * cmile) // 3600
minute = (start + warmup * wmile + tempo * tmile + cooloff * cmile - hour * 3600) // 60
second = (start + warmup * wmile + tempo * tmile + cooloff * cmile - hour * 3600) % 60
print('Your run ended at %02d:%02d:%02d' % (hour, minute, second))
In this code, the time prompts are all the same: "Enter time (hh:mm:ss):" I want each prompt to refer to its variable name, e.g., "Enter start time (hh:mm:ss)" or "Enter time (hh:mm:ss): (warmup)". Is there a way to do this?
Note: While this may technically be a duplicate, I have examined the similar questions, but decided that both the questions and the answers provided were on the more unspecific side, and therefore decided to ask my question anyway.
Yes, use the input to your function elapsed(t).
Right now it's being overwritten with the return from raw_input()
def elapsed(t):
t1 = raw_input('Enter time (hh:mm:ss): ({0})'.format(t))
th, tm, ts = t1.split(':')
return int(ts) + int(tm) * 60 + int(th) * 3600
or
def elapsed(t):
t1 = raw_input('Enter time (hh:mm:ss): (%s)' % t))
th, tm, ts = t1.split(':')
return int(ts) + int(tm) * 60 + int(th) * 3600
def enter_time(specify_string):
print("Please enter the", specify_string, 'time in format hh:mm:ss > ', end='')
hh, mm, ss = input().split(':')
return (hh, mm, ss)
start_time = enter_time('start')
finish_time = enter_time('finish')
>>>Please enter the start time in format hh:mm:ss >13:32:34
>>>Please enter the finish time in format hh:mm:ss >12:21:21
>>>start_time
(13, 32, 34)
Now you can call the function with string arguments in the function call, and it will generalise the function for different requirements.
It is better to move the time between functions in a more readable format, such as a tuple. You can make more functions, eg:
- Test valid time entered
- Convert tuple to seconds for calculations
- Convert seconds back to tuple format
ect.
Let me know if I misunderstood you.
I'm trying to work out a way to make a new variable AUTOMATIC_END_TIME based on adding the minimum amount of time onto the start time but I can't figure out the way to allow START_TIME to be turned into a time that can then have time added onto it.
So far my script has the following:
from time import sleep
from datetime import datetime, timedelta
START_TIME = "19:18"
END_TIME = "19:25"
LOGA = ["one", "two"]
TIME_DIFFERENCE = datetime.strptime(END_TIME, "%H:%M") - datetime.strptime(START_TIME, "%H:%M")
TIME_DIFFERENCE = TIME_DIFFERENCE.seconds
if len(LOGA) * (60 * (5 + 1)) >= TIME_DIFFERENCE:
print "Show minimum end time"
AUTOMATIC_END_TIME = "" # Should come out as 19:30
The current script shouldn't change at all except for AUTOMATIC_END_TIME which should be START_TIME + (60 * (5 + 1) It should come out as 19:30
>>> (datetime.strptime(START_TIME, "%H:%M") + timedelta(minutes=12)).strftime('%H:%M')
'19:30'
from time import sleep
from datetime import datetime, timedelta
START_TIME = "19:18"
END_TIME = "19:25"
LOGA = ["one", "two"]
TIME_DIFFERENCE = datetime.strptime(END_TIME, "%H:%M") - datetime.strptime(START_TIME, "%H:%M")
TIME_DIFFERENCE = TIME_DIFFERENCE.seconds
if len(LOGA) * (60 * (5 + 1)) >= TIME_DIFFERENCE:
print "Show minimum end time"
AUTOMATIC_END_TIME = (datetime.strptime(START_TIME, "%H:%M") + timedelta(minutes=12)).strftime('%H:%M')
print AUTOMATIC_END_TIME