Tell your fortune with your own Magic 8 Ball
Do you have important questions that need answering, but have no friends and your family hates you? Life can be tough, but lucky for you, you’re a programmer. You can code your own damn friends and even answers too! With this magic 8 ball script written in python, you will be able to ask any question you’d like and receive a prompt response.
Download this python script on Github.
How It Works
Ok so first we import the random module to help us choose a different answer each time the script is ran.
In the magic8Ball() function, we are asked to type in a question and we store it into the variable x. Next in answerList, we store all the magic 8 ball responses in an array format. An array is a special variable where you can hold more than one value at a time.
Then we created the randomAnswer variable which just chooses a random value from the answerList array. Feel free to change the responses as you’d like, to get a more desirable answer to your question.
Next we enter a if else statement. If you do not ask a question, the program will let you know and it will exit. If you did ask a question, you will receive a response and then given the option to ask another question with the askAgain() function.
The askAgain() function simply ask if you’d like to ask another question. If you say yes, then the magic8Ball() function is called again and you start all over, else the program exits.
Lets run it now and see what we get.
And just like that my dreams are crushed. Thanks for reading and once again feel free to leave a comment below with any suggestions or questions.
Want to get better in Python?
Well come on over and take a look at “Automate the Boring Stuff with Python: Practical Programming for Total Beginners“. If you’ve ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do them for you?
In Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required. Once you’ve mastered the basics of programming, you’ll create Python programs that effortlessly perform useful and impressive feats of automation to:
–Search for text in a file or across multiple files
–Create, update, move, and rename files and folders
–Search the Web and download online content
–Update and format data in Excel spreadsheets of any size
–Split, merge, watermark, and encrypt PDFs
–Send reminder emails and text notifications
–Fill out online forms
Disclaimer: The programs in this book are written to run on Python 3.
This post “Magic 8 Ball Script written in Python” is located under the Programming Category