Record#
- Today I learned how to use the os and time libraries, but it's not really learning, just learning one usage in these two libraries.
- Import multiple libraries, separate them with commas.
- os.system is a function that executes operating system commands. For example, os.system("clear") will clear the screen.
- time.sleep is a function that makes the program wait. For example, time.sleep(3) will wait for 3 seconds before executing the next statement.
- Today's exercise is to imitate a music player, which is quite difficult for me. It took me about 20 minutes to write it. I still have a lot to learn about the usage of input.
- I learned a usage of input(), you can directly call it without any content in the parentheses. This way, the 3 prompts in the player can be printed, and then use input to receive user input.
CODE#
from replit import audio
import os, time
print("🎵 MyPOD Music Player")
def play(pt):
source = audio.play_file('audio.wav')
source.paused = False # unpause the playback
while True:
pt = input(
"Press 1 to Play . Press 2 to Exit . Press anything else to see the menu again."
)
if pt == "2":
source.paused = True
break
# Start taking user input and doing something with it
#input("Press 1 to Play , Press 2 to Exit ,Press anything else to see the menu again.")
while True:
# clear the screen
os.system("clear")
# Show the menu
print("🎵 MyPOD Music Player")
# take user's input
pt = input(
"Press 1 to Play . Press 2 to Exit . Press anything else to see the menu again."
)
play(pt)
# check whether you should call the play() subroutine depending on user's input
Translation: