How we do some fun with python

How to do some fun with Python

Python is a straightforward and easily understandable language, for out of India, python is a highly demandable language.

Python can ease the way of programming that is very good for us, and by this, the development in programming is increasing day by day.

For development, python is very useful, but in another way, python is also used for fun.

Due to its easy-to-understand property, we just try to make some fun with Python.

Mood type music player

Everyone in this world has a common thing, but different types like anyone have good mood because they got good marks in their exam.

Anyone has sad mood because he/she did not crack the exam.

So, mood is very important for every person it show the condition of the person.

By the help of python, I just create a program that play the songs according to user’s mood.

Code of Program

import pyttsx3
import random
import os
def speak(audio):#This function is use to give permission for speak by AI
    engine = pyttsx3.init('sapi5')  # activate engine
    voices = engine.getProperty('voices')
    # print(voices[0].id) use for print the assistant name
    engine.setProperty('voice', voices[1].id)
    engine.setProperty("rate", 150)
    engine.say(audio)
    engine.runAndWait()
speak("Hello sir, I am Edith, welcome to Mood Type Music Player")
print("Mood Music Player")
speak("sir How's your mood")
mood = str(input())
if mood == "sad":
    speak("ooh! I have something for you, there we go")
    music_dir = "Here put the path of the folder of song"
    songs = os.listdir(music_dir)
    sd = random.choice(songs)
    os.startfile(os.path.join(music_dir, sd))

elif mood == "happy":
    speak("great! I have something for you,  there we go")
     music_dir = "Here put the path of the folder of song"

    songs = os.listdir(music_dir)
    sd = random.choice(songs)
    os.startfile(os.path.join(music_dir, sd))

elif mood == "workout":
    speak("good! I have something for you,  there we go")
    music_dir = "Here put the path of the folder of song"
    songs = os.listdir(music_dir)
    sd = random.choice(songs)
    os.startfile(os.path.join(music_dir, sd))

elif mood == "chill":
    speak("aaha! I have something for you,  there we go")
    music_dir = "C:\\Users\\Prakhar Tripathi\\PycharmProjects\\Python\\SURU AI\\AUDIOS\\Selena gomez"
    songs = os.listdir(music_dir)
    sd = random.choice(songs)
    os.startfile(os.path.join(music_dir, sd))

elif mood == "worship":
    speak("okey! I have something for you,  there we go")
    music_dir = "Here put the path of the folder of song"
    songs = os.listdir(music_dir)
    sd = random.choice(songs)
    os.startfile(os.path.join(music_dir, sd))

else:
    speak("sorry, i can't understand")

 

Modules

pyttsx3: This module is used for text to speech; this is used to speak the text written by us in the program.

Random: This module is used to choose the random songs from the folder.

Os: This module is used for operating systems.

Functions

def(speak): This is a defined function used for the speech of text messages in the program.

Print (): This function is used to print the statement that is present in it.

Input (): This function is used to take input.

Part of the function

def speak(audio):

  • engine = pyttsx3.init(): This part is used to initiate the pyttsx3 and put it in a variable name
  • voices = engine.getProperty('voices'): This Part of the function is used to get the property of voice and put it into the variable name
  • setProperty('voice,' voices[1].id): This part of the function is used to set the voice by setting a male or female voice that can speak the message.
  • setProperty("rate," 150): This is used to set the rate of the voice means the speed of the voice, at which speed the message is spoken by the system.
  • Say (audio): This allows the program to speak the written message during the run. Time of the program.
  • Run and wait(): This is used to execute all the parts of the pyttsx3.

Os.listdir(): This part is used to make the list of the directory. That’s the path you put in the bracket.

Os.start file(): This part is used to start the program using the operating system.

Random. Choice (): This part allows the computer to choose any random song from the directory or folder.

The output of the Program:

First, it prints the player’s name

After that, it asks you about your mood

Same like this

After that, it can give you voice notation according to your mood

Then play the music.

 

Try this program on your laptop or desktop by using any idea; I recommend PyCharm.

Click here to download it: https://www.jetbrains.com/pycharm/download/#section=windows\

Note: Choose community if you want it for free; if you want to be paid, then choose professional

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

About Author