DEV Community

GrimKillingbeck
GrimKillingbeck

Posted on

1

Day 10 of Python : The Magic 8-Ball

This is going to be a long post, so just warning you now.

I am a beginner and I am doing my best to not only code, but learn how to explain my code properly, so please be patient with me. Also, I tried to add manual line breaks but it did not register so that I could add breaks in between text.

I started Codecademy about 10 days ago for structured learning of Python and I've also been reading Automate The Boring Stuff so that I can reinforce what I've learned with small projects from the book.

This Codecademy project involves creating a 80's toy "Magic 8-Ball", where you would ask it a question, flip the ball over and then up would float the answer to your question.

The original project required me to assign someone's name to the variable (name) and then assign a yes or no question to another variable (question). We then assigned an empty string to our variable called "answer" to store a random answer, which is decided by the random module we imported.

#assign name of the person talking to the 8 ball
name = "Makeda"
# assign planned question to ask 8 ball
question = "Will I  get a cybersecurity job this year?"

#leaving string empty for now , but should store potential answers 
answer = ""

#importing random module so that I can assign the potential answers
import random

# assigned randomly generated value to a variable. Arguments for the function will be 1 and 9 but 1,9 means 1-9 as I have 9 potential answers

random_number = random.randint(1, 9)
Enter fullscreen mode Exit fullscreen mode

Then I tested the random module to see if it was working as it should:


#prints the current value of random_number
print(random_number)
# used the above callto function just to see if the module  worked as intended 

Enter fullscreen mode Exit fullscreen mode

Which printed out random numbers such as 7, 2 ,3, etc.

Here, I created if, elif and an else statement that addresses each possible value for the variable random_number which holds the function call random.randint(1,9) , to generate a random number from 1 to 9 . Each statement says if the number currently stored in random_number is equal to let's say 7 , then the string assigned to the variable answer is whatever the message is I have assigned for that number.


if random_number == 1 :
  answer = "Yes- definitely"
elif random_number == 2 :
    answer = "It is decidedly so"
elif random_number == 3 :
    answer = "Without a doubt"
elif random_number == 4 :
    answer = "Reply hazy,try again"
elif random_number == 5 :
    answer = "Ask again later"
elif random_number == 6 :
    answer = "Better not tell you now"
elif random_number == 7 :
    answer = "My sources say no"
elif random_number == 8 :
    answer = "Outlook not so good"
elif random_number == 9 :
    answer = "Very doubtful"
else :
    answer = "Error" #in case somehow number out of the range is chosen 
Enter fullscreen mode Exit fullscreen mode

Now that I've established my variables, the module needed to random answers and my control flow, now I want to print out the result :


print(name, " asks: " , question)
print("Magic 8-Ball's answer : ", answer)
Enter fullscreen mode Exit fullscreen mode

Output :

Makeda asks: Will I get a cybersecurity job this year?
Magic 8-Ball's answer : Reply hazy,try again

While completing this project, I happened to be learning about while loops, continue and break. I thought about how I could improve the Magic 8-Ball and add a bit of my personality into it.

Using the input() function instead of assigning a name and question manually

One of my goals was to have a greeting that would prompt the user to enter their name. I created an infinite while loop that would continue to print the statement : "I'm here to provide answers to your most dire questions. What is your name?" unless there was a break to the loop.

Instead of assigning a string to name manually, I assigned the input() so that the user would store their name as a string.

while True:
    print("\nI'm here to provide answers to your most dire questions. What is your name?")
    #INPUT user name
    name = input()
Enter fullscreen mode Exit fullscreen mode

Magic 8-Ball greeting

But what if they didn't enter their name?

Here is where I started to have fun, because I was thinking of how an actual human would respond to someone asking for their fortune to be read.

I created an if statement that says if the variable name has an empty string, print : "Okay, you don't want me to know your name. What is your question?" or else print "Welcome, (name), what is your question?"

 if name == "" :
        print("Okay, you don't want me to know your name. What is your question?")
    else :
        print("\nWelcome, ", name ,". What is your question?")
Enter fullscreen mode Exit fullscreen mode

Because I did not enter a name, the Magic 8-Ball answered,



Of course, I wanted the user to input their own question like their name, so I assigned the input() function to the variable (question).

What if the user didn't enter a name or a question?



This part is was the most annoying because I learned you must talk to the computer as if it's a baby; step by step, nothing is implied.

Originally, I set up my statement as such :

if name and question == "" :
Enter fullscreen mode Exit fullscreen mode

but again -- baby talk. Check is name if equal to an empty string AND check if question is equal to an empty string.

if name == "" and question == "" :
Enter fullscreen mode Exit fullscreen mode

In any case, I wanted to address a user that either did not want to give their name and maybe couldn't think of a question. If they both the variables name and question were empy strings, the program is of no use, so I ask the user to comeback with a question and used the statement continue to restart the script.

if name == "" and question == "" :
        print("You're being mighty secretive for someone who wants to ask about the future. Come back when you want to give your name and a question. \n")
        continue
Enter fullscreen mode Exit fullscreen mode

The next possibility is that the user may have opted to leave out their name, but had a question for the Magic 8-ball :

elif name == "" and question != "" : #if the name has an empty string but the question doesn't 
        print ("You asked: ", question, "\n", "Magic 8 Ball says: " , answer) #prints question with no name and gives random answer

Enter fullscreen mode Exit fullscreen mode



Here is the variable (name) is equal to an empty string and the variable question is NOT equal to an empty string, the program addresses the user as "you", and then gives a random answer.

Screenshot of the Magic 8-Ball saying : You asked :

What if they give a name, but they decide they don't have a question?

Well (name), GTFO.


elif name != "" and question == "": #is user entered name but leaves question blank, will send message and go back to the beginning of script
        print("Ask me something ", name , " or GTFO!")
        continue
Enter fullscreen mode Exit fullscreen mode

The continue statement then restarts the script.

Screenshot of Magic 8-Ball saying



At this point, I've addressed all the possible exceptions that I can think of (at the moment) and moved on with an else statement that assumes th user input a string for both variables :

else :
        print(name , "asked: ", question, "\nMagic 8 Ball says: " , answer) #if name and question has been entered

Enter fullscreen mode Exit fullscreen mode

Finally, since most of the control flow was set up I wanted the user to decided if they wanted to ask another question or leave the program.

I assigned the input() function to variable (to_be_continued) and if the string was equal "Yes", then it would print "Here we go again!" and restart the script, or if it is equal to "No", the magic 8-Ball responds with "Good Riddance" follow by a break of the loop.

Screenshot of the program run with both inputs for name and answer. I chose NO when it asked if I wanted to continue and the Magic 8-Ball said



So what do you think? What could I do to make this program better?

What would you add to make the Magic 8-Ball more fun?

Thanks for reading!

Top comments (0)