Python Micro-Project | Fortune Teller Using Random Module.

 Fortune Teller using Python Module "random"

Today we will create a Fortune Teller that will tell your Future. This Fortune Teller will give an answer to your questions. We will create a list that will contain all the answers & we will select any random answer to your question.

What Modules will be used?

For this Fortune Teller, the basic modules that will be used are:
  • Random Module 
  • pyttsx3 Module (optional)
Random Module will be used for choosing any random answers for the ansList. Specifically, we will be using the choice(sequence), here sequence can be any list, tuple, string. It return any random element from the sequence.

pyttsx3 Module will be used to convert the text answer to speech answer. This will an optional module to use in the program, I'm using coz it makes my program a step forward.

Code:

import random
import pyttsx3 #optional
engine = pyttsx3.init()
ansLIst = ['OK','NO','Not Sure']#programmer can add more different answer

def fortune():
  ques = input("Ask a Question-->")
  ans = random.choice(ansList)
  engine.say(ans)
  engine.runAndWait()
  Ask()

def Ask(): #function to ask next question
  ask = input('Want to ask next Question[Y/N]-->')
  if ask == 'Y':
    Fortune()
  elif == 'N':
    engine.say('Have a good day :)')
    exit()
  else:
   engine.say('Can't Recognize...')
   Ask()

So this is the code for Fortune Teller. In the above, code we have created a Fortune() function that ask for a question and select any random answer from ansList, you can add more & different answer in ansList. Then Ask(), function ask the user that he wants to ask the next question or want to exit.

If you don't know what engine.say(), function do then do check my previous blogs I have made a program and explanation.

Also, search programs & other blogs on my website.

Follow for such blogs, also Follow on my Instagram <--Click here

Also Check my Portfolio: Prathmesh Chaudhari

Comments