In [9]:
#importing randint() from the random library
from random import randint

#importing the clear_output() from IPython.display library
from IPython.display import clear_output

#importing the sleep() from time library
from time import sleep

#the list of responses
response = ["As I see it, yes.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don’t count on it.", "It is certain.", "It is decidedly so.", "Most likely.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Outlook good.", "Reply hazy, try again.", "Signs point to yes.", "Very doubtful.", "Without a doubt.", "Yes.", "Yes - definitely.", "You may rely on it."]

#function that ask if you want to leave
def leave():
    exitQuestion = input("would you like to try again? Y,y or N,n ==>")
    if exitQuestion == "Y" or exitQuestion == "y":
        main()
    elif exitQuestion == "N" or exitQuestion == "n":
        clear_output()
        print("have a good day")
    else:
        leave()
    
# main fuction that works as the magic eight ball    
def main():
    
#sleep timer do it looks like the fuction is thinking
    sleepTimer = randint(2,4)
    
    #responseitem generates a random number from the response list
    responseItem = randint(0,19)
    
    #this asks what your is that you would like answered
    question = input("what is the question you would like answered?")
    
    #if the question is left puts a generic response
    if question == "":
        question = "The answer is."
        
    #clears screen
    clear_output()
    
    print("And your answer is...")
    
    #tells the thing to wait 2 to 4 seconds
    sleep(sleepTimer)
    
    clear_output()
    
    #prints answer
    print("{} \n".format(question))
    print(response[responseItem])
    
    #ask if you want to leave
    leave()
    
main()
have a good day
In [ ]:
 
In [ ]: