Skip to content

Commit da8b23a

Browse files
committed
add magic 8 ball
1 parent 82a1e93 commit da8b23a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

projects/Magic8Ball.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Magic 8 ball.
2+
# By Ted Silbernagel
3+
4+
# Import dependencies
5+
from random import randint
6+
7+
# Define main function
8+
def magic8Ball(question):
9+
# Set up array to store responses and values
10+
# Answers taken from https://en.wikipedia.org/wiki/Magic_8-Ball#Possible_answers
11+
responses = [ ['It is certain.', 'positive'],
12+
['It is decidedly so.', 'positive'],
13+
['Without a doubt.', 'positive'],
14+
['Yes - definitely.', 'positive'],
15+
['You may rely on it.', 'positive'],
16+
['As I see it, yes.', 'positive'],
17+
['Most likely.', 'positive'],
18+
['Outlook good.', 'positive'],
19+
['Yes.', 'positive', 'positive'],
20+
['Signs point to yes.', 'positive'],
21+
22+
['Reply hazy, try again.', 'non-committal'],
23+
['Ask again later.', 'non-committal'],
24+
['Better not tell you now.', 'non-committal'],
25+
['Cannot predict now.', 'non-committal'],
26+
['Concentrate and ask again.', 'non-committal'],
27+
28+
['Don\'t count on it.', 'negative'],
29+
['My reply is no.', 'negative'],
30+
['My sources say no.', 'negative'],
31+
['Outlook not so good.', 'negative'],
32+
['Very doubtful.', 'negative']
33+
]
34+
35+
# Choose a random response
36+
chosenResponse = responses[randint(0, len(responses) - 1)]
37+
38+
# Compile data into dict for returning
39+
returnValues = {}
40+
returnValues['question'] = question
41+
returnValues['answer'] = chosenResponse[0]
42+
returnValues['value'] = chosenResponse[1]
43+
44+
# Return data
45+
return returnValues
46+
47+
# Get user input
48+
print('Magic 8-ball...')
49+
userQuestion = input('Please enter your question: ')
50+
51+
# Call function
52+
response = magic8Ball(userQuestion)
53+
54+
# Print response for user
55+
print(f'Your question is: \"{response["question"]}\"')
56+
print(f'The Magic 8-ball\'s answer: \"{response["answer"]}\" ({response["value"]})')

0 commit comments

Comments
 (0)