2

I tried to make a timer, here is my code

import time

timer = int(input("how long"))
s = 1
w = int(input("what is it"))
while s < w + 1:
    print (s)
    s = s + 1
time.sleep(10)
print ("your timer is done")

then sound loud beep`

jojm
  • 47
  • 1
  • 10
  • Not exactly related, but here's how to play a sound: https://stackoverflow.com/a/45260700/7553525 (last example) – zwer Jul 28 '17 at 15:18

3 Answers3

2

first install the library

pip3 install pyttsx3

then execute

python3 say_it_pyttsx3.py

cat say_it_pyttsx3.py

import pyttsx3 as pyttsx

what2Say = '\
    your timer is done \
    '

# Speaking engine
speakEngine = pyttsx.init()
speakEngine.say(what2Say)
speakEngine.runAndWait()
Scott Stensland
  • 22,853
  • 10
  • 81
  • 88
1

How to give out a beep depends greatly on your underlying system. You could try by simply print('\a') which is supposed to do that.

Unfortunately, a lot of modern systems do not stick to that old rule. Then you could try to tweak your system to honor the "bell" character or you could try to give out a beep using a completely different method, e. g. by playing a small sound file.

How to do this again depends greatly on your operating system etc. You might want to tell us more about this.

On my Linux box, for instance, I use a commandline tool called play (from package sox) to play .wav and .ogg files, and mpg123 to play .mp3 files. From Python you can start them e. g. using this:

import subprocess
subprocess.call([
    'play', '-q', '/usr/share/sounds/KDE-K3B-Finish-Success.ogg' ],
  env={'AUDIODEV':'plughw:1,0'} )
Alfe
  • 47,603
  • 17
  • 84
  • 139
  • I tried this but here is what happened. how long0 what is it1 1 your timer is done >>> no sound? – jojm Jul 28 '17 at 18:58
  • File not found. How to use full path? – Peter.k Apr 09 '19 at 14:10
  • I guess you have no `play` installed. What does `play` on the command line result in? I installed it in Ubuntu by `sudo apt-get install sox`. – Alfe Apr 09 '19 at 16:07
0

This has similarly been answered here. In essence, you would need a third party library, but which you need depends on the type of sound you want to play, if you just want a basic beep or a supplied audio file, and multiple other factors, but they suggest Snack as a starting point. There is installation instructions on Snack's site and the linked answer suggests usage as simple as:

import snack
s = Sound() 
s.read('sound.wav') 
s.play()
Nathan Smith
  • 643
  • 1
  • 11
  • 21