WakeMeUp
So I'm human in case you were wondering and like all humans I suffer from the old age disease called procrastination! Now it's not that I enjoy procrastinating, but I do enjoy the activities that I do while procrastinating. It's not the same. Why do I say so? Simple. My thought process is as follows, Procrastination BAD BAD. Watching Hannibal instead of practising my programming? BAD DOG BUT CAN I PET THAT DAWG. Yeah, weird TikTok reference aside this gave me an idea for the next project I will attempt an alarm clock. Nothing like the good old sound of well something to remind you how much time you've wasted. As usual, I went to YouTube to find a video. No, this isn't me going back to tutorial hell, I just don't know how to use the DateTime modules in Python.
So the first thing is adding the sound well it can't be an alarm if it's silent can it?(yes, this is to all of you who set alarms only to put your phones on DND). Yes, you guessed it Kanye Fan so that song is the one, I know I know.
I enjoy it when I have to use cmd, makes me feel all hackery. So you open up cmd and run this command if you're using Spyder and not sure about other IDEs
So before I started writing lines of code, I wanted to test out this (and maybe I wanted to play the song too). So imported the playsound function from the playsound module
So a 1, a 2, and a 1 2 3
So the first thing is adding the sound well it can't be an alarm if it's silent can it?(yes, this is to all of you who set alarms only to put your phones on DND). Yes, you guessed it Kanye Fan so that song is the one, I know I know.
add the song to the folder of the Python script.
Then we have to install the module required to play the sound. Now we use the terminal for this, but I use Spyder and I don't have it installed in there, this just shows how far in tutorial hell I was in, no modules installed so if you're like me the next step is.
install the terminal
conda install spyder-terminal -c conda-forge
I used my phone to take the screenshot because my laptop was refusing to do it on its own? Need a new laptop ASAP.
it worked, I think I'm getting good at this. So now we can install the module to play the sound
Installing the module:
In the terminal, I ran the command pip install playsound
The Alarm:
So before I started writing lines of code, I wanted to test out this (and maybe I wanted to play the song too). So imported the playsound function from the playsound module
Now that's not the sound of Ye and if you've been here long enough you'll know that here we are firm believers of trial and error. I never seem to program without having such problems. So now being accustomed to this I now know how to handle it, so I copied the error, went to Google and prayed for a miracle. Which I found wasn't necessary as I for the first time wasn't the problem.
Turns out the update in the module caused errors for not just me, yes I am not the problem. So I have to uninstall and install a specific version that doesn't have such issues.
So did that, and it worked. Ran my pre-code and It WORKED, IT WORKED. Good morning wooohohohohohohoooo.
Back to the Alarm
So that's the code. I'll briefly explain it. Line 6 and 7? Don't fully understand it, but apparently, it clears the terminal and allat. okay? Line 9 is our function which takes seconds as the parameter. We initialise time elapsed as 0 because we need a variable to keep track of that. A while loop follows that checks if the time elapsed is still less than the time we gave as the parameter. In that loop we have time. sleep which is what tells the computer how much time must pass before it lowers the number, yes computers aren't that smart. So we set it to 1 which is 1 second from the module and then we increase time elapsed. I am sure lines 17 to 19 make sense, if not then someone needs to brush up on Python basics on what those symbols do. We printed using an f string because the value changes each time yeah. then we play our lovely song.
After that, we ask the user to input the time they want to wait and then we start, but of course, you can't take my word for it that it works so head on over to the Instagram account to see it in action
BLOOPERS: As much as I love my song what I didn't think about is that Python plays the sound till the end, that song is 3 minutes long so I had to restart my terminal each timeeπππ











Comments
Post a Comment
Say something Diego!