0

I want to create a loop that prints:

"The train will leave at 13:36"
"The train will leave at 13:56"
"The train will leave at 14:16"
"The train will leave at 14:36"
"The train will leave at 14:56"
"The train will leave at 15:16"
etc. etc...

I have a code that says:

h = 13 
m = 36

for i in range(5):
    print("The train will leave at {}:{} ".format(h,m))
    m = m + 20

    if 60 <= m:
        break
    print("The train will leave at {}:{} ".format(h,m))
    h = h+1
    m = m-60+20

Output is:

The train will leave at 13:36 
The train will leave at 13:56 
The train will leave at 14:16 
The train will leave at 14:36 
The train will leave at 15:-4 
The train will leave at 15:16 
The train will leave at 16:-24 
The train will leave at 16:-4
The train will leave at 17:-44
The train will leave at 17:-24

How can I fix it so minutes increments by 20 minutes and every time it reaches 60 minutes it should output the right time...

5 Answers 5

1

Use binary arithmetic operations for handling minutes and hours, namely modulo operator % and floor division //:

h = 13 
m = 36

for i in range(10):
    print("The train will leave at {}:{} ".format(h,m))
    h = h+((m+20)//60)
    m = (m+20)%60
    if h == 24:
        h = 0

Note the last two lines: you should check the hour value in order to come back to 0 when h==24.

Sign up to request clarification or add additional context in comments.

1 Comment

Use divmod function
1

You can use datetime module from standard library:

from datetime import timedelta, datetime

t = datetime(hour=13, minute=36, year=2019, month=6, day=9)

for i in range(5):
    print("The train will leave at {}:{} ".format(t.hour,t.minute))
    t += timedelta(minutes=20)

Prints:

The train will leave at 13:36 
The train will leave at 13:56 
The train will leave at 14:16 
The train will leave at 14:36 
The train will leave at 14:56 

Comments

0

A slight modified version of your code. Just check for the minutes using if. In case, minutes are more than 60, increase the hour by one and rewind the minutes back by subtracting 60

h = 13 
m = 36

for i in range(5):
    if 60 <= m:
        m = m-60
        h = h+1
    print("The train will leave at {}:{} ".format(h,m))       
    m = m + 20

# The train will leave at 13:36 
# The train will leave at 13:56 
# The train will leave at 14:16 
# The train will leave at 14:36 
# The train will leave at 14:56     

Comments

0

You should give the % operator a try.

The % operator is the modulo operator which you can easily think of as an operator that provides the remainder of the division between the left operand and the right one. Defining this operator can get a bit more technical if you did some group theory or abstract algebra in college.

Basically start at a given minute and increment by 20 minutes. Whenever the number of minutes passes 60 minutes then we need to increment the hour. But we need to reset the number of minutes. So the first time m reaches 76, we set it back to 76 - (1 * 60) = 16. Note that m will never go past 120 unless it is set to a number larger than 99 from the get go.

What I think you really want is this though

h = 13 
m = 36

for i in range(5):
    print("The train will leave at {}:{} ".format(h,m))
    m = m + 20
    if  m >= 60:
        h += 1
        m = m % 60

Output

# The train will leave at 13:36 
# The train will leave at 13:56 
# The train will leave at 14:16 
# The train will leave at 14:36 
# The train will leave at 14:56 

But you should probably also account for when h goes past 24. so if h >= 24: h = 0.

Once again the assumption is that you always begin with m below 60.

Comments

0

Use the following if statement to check for hour change, I have changed the hour and minutes for double checking

h = 15 
m = 53

for i in range(5):
    print("The train will leave at {}:{} ".format(h,m))
    m = m + 20
    if m >= 60:
        h = h+1
        m-=60
        print("The train will leave at {}:{} ".format(h,m))
        m = m + 20

The execution will be:

The train will leave at 15:53 
The train will leave at 16:13 
The train will leave at 16:33 
The train will leave at 16:53 
The train will leave at 17:13 
The train will leave at 17:33 
The train will leave at 17:53 
The train will leave at 18:13

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.