Jump to content

Timer stops before I type the exit code Python

<

import time
import winsound

def countdown(t):
    while t:
        mins, secs, = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\n")
        time.sleep(1)
        t -= 1

    print('Timer completed!')

t = input('Enter the time in seconds: ')

def beep():
    winsound.Beep(700, 1000)
    time.sleep(0.75)

countdown(int(t))
while True:
    beep()

    if input() == "stop":
        exit()

>

This is meant to constantly beep but it only beeps once and if I take the if statement away it constantly beeps, how can I get it to constantly beep with the exit code?

 

Link to post
Share on other sites

countdown(int(t)) will run its entire loop before the 'while True:' loop is run.

Your program asks for a time in seconds, counts down from that time, then beeps once and exits. This is like a kitchen timer program - set the time, then get a beep at the end.

 

By "constantly beep" do you mean to start beeping as soon as you begin the program? If so, you'd do better to put it inside the timer loop.

Link to post
Share on other sites

17 hours ago, Enderg312 said:

how can I get it to constantly beep with the exit code?

You'd need to spawn a separate thread or process that beeps.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to post
Share on other sites

if you use sleep in your main process, the process will sleep for 1s and you won't do anything with your input.

 

Look into async  stuff :  https://stackoverflow.com/questions/45419723/python-timer-with-asyncio-coroutine

 

// not a Python programmer but I'm familiar with the concept from other programming languages (ex javascript, microcontroller interrupts etc)

Link to post
Share on other sites

19 hours ago, Enderg312 said:

It does beep I want to make a exit code that ends the beeping after the user knows the timer is going off.

Yes, that's why you would use separate threads for the beep and the main loop, for example. Currently that while loop will just beep, check for input, beep if input is not stop, check for input etc. A separate thread will run independent from the main loop and will allow you to beep while checking for the input to be stop. The threading module will allow you to create separate threads (but still limited on the same core) and multiprocessing, for example, will allow you to leverage multiple cores by spawning a new process.

 

If you're on Python 3.7 you may also try the above suggestion using asyncio (I acutally didn't know about this one yet, it seems pretty neat).

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×