Play sound in a repl with Python
RojerGS

Hello everyone,

In light of the new challenge I was going to create something with Python (and pygame) and it would involve playing sounds, but turns out I cannot use the pygame.mixer module. Is it because I am a n00b?
Either way, is there any (relatively simple) way of playing audio in a Python repl?

Thanks!

You are viewing a single comment. View All
Answered by 21natzil [earned 5 cycles]
View Answer
26pwong

@MarkusStepanovs Instead of printing it repeatedly, you can use a loop.
This is what I made:

import time

def beep():
print ("\a")

for i in range(10):
beep()
time.sleep(0.1)

It works, but it prints a new line every time it plays the sound. Anyone know a better solution?

alan_oregano

@26pwong

This sets the end argument of the print statement to a carriage return escape sequence.
Moving it back to the start of the line instead of a new one by default.

You could also add parameters to the function to control the interval and frequency of the bell/alert tone.