Skip to content

Commit bee22ba

Browse files
Merge pull request prathimacode-hub#1041 from yashh18/main
Magic 8-Ball
2 parents 8f8652c + 125ea80 commit bee22ba

4 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Import the modules
2+
import sys
3+
import random
4+
5+
ans = True
6+
7+
while ans:
8+
question = input("Ask the magic 8 ball a question: (press enter to quit) ")
9+
10+
answers = random.randint(1,8)
11+
12+
if question == "":
13+
sys.exit()
14+
15+
elif answers == 1:
16+
print("It is certain")
17+
18+
elif answers == 2:
19+
print("Outlook good")
20+
21+
elif answers == 3:
22+
print("You may rely on it")
23+
24+
elif answers == 4:
25+
print("Ask again later")
26+
27+
elif answers == 5:
28+
print("Concentrate and ask again")
29+
30+
elif answers == 6:
31+
print("Reply hazy, try again")
32+
33+
elif answers == 7:
34+
print("My reply is no")
35+
36+
elif answers == 8:
37+
print("My sources say no")
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# MAGIC 8 BALL
2+
3+
## AIM
4+
To generate random answers to questions asked to the Magic 8 Ball
5+
6+
## PURPOSE
7+
The Magic 8 Ball is a toy used for fortune-telling or seeking advice.
8+
9+
## MODULES USED
10+
Sys : The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.
11+
12+
Random : This module implements pseudo-random number generators for various distributions.
13+
For integers, there is uniform selection from a range. For sequences, there is uniform selection of a random element, a function to generate a random permutation of a list in-place, and a function for random sampling without replacement.
14+
15+
## WORKFLOW
16+
- First of all, we would import sys and random modules from the python library
17+
- Declare a boolean value(True) to ans variable
18+
- Setup a while loop which works until the value remains True
19+
- Ask any random question to which we want answer
20+
- Using randint func generate random a random number from 1-8
21+
- Now using elif ladder, generate a random answer based on the question and print it
22+
23+
## SETUP INSTRUCTIONS
24+
User needs a Python IDE to run the program
25+
26+
## OUTPUT
27+
![Code](Images/Magic_8_Ball.py - Magic 8-Ball - Visual Studio Code 12_10_2021 2_33_43 pm.png)
28+
29+
30+
![Output](Images/Magic_8_Ball.py - Magic 8-Ball - Visual Studio Code 12_10_2021 2_34_33 pm.png)
31+
32+
## AUTHOR
33+
YASH KATARIA
34+
35+
## FOR ANY DOUBTS
36+
www.linkedin.com/in/kataria-yash

0 commit comments

Comments
 (0)