mardi 12 janvier 2021

While function stuck on first block of if-elif-else

I'm trying to code a small pomodoro-timer, it uses a while loop with an if-elif-else statement to check which timer to start.

As expected it starts with the first if-block and then modifies the variable, I would expect it to go the elif-block after that, however it seems to be stuck in the if-block. And doesn't reiterate the entire while loop code.

How to overcome this?

import os
import time

pomodoro = ['Pomodoro', 1500]
short_break = ['Short Break', 300]
long_break = ['Long Break', 1800]
pomodori_count = 0

def countdown(time_frame):
    duration = time_frame[1]
    while duration:
        os.system('clear')

        mins, secs = divmod(duration, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)

        print(f"{time_frame[0]} | {timer}")
        time.sleep(1)
        duration -= 1

while True:
    last = ""

    if last != "Pomodoro":
        countdown(pomodoro)
        last = "Pomodoro"
        pomodori_count += 1
    elif pomodori_count % 4 != 0:
        countdown(short_break)
        last = "Short Break"
    else:
        countdown(long_break)
        last = "Long Break"

Aucun commentaire:

Enregistrer un commentaire