Question

In: Computer Science

Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...

Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask a question, then display one of the responses, randomly selected from the list. The program should repeat until the user is ready to quit.

use the template below:

def ___ function:

# open a file

#__ _ _ _ _

#close file

___( )


Contents of 8_ball_responses.txt:

Yes, of course!
Without a doubt, yes.
You can count on it.
For sure!
Ask me later.
Iím not sure.
I canít tell you right now.
Iíll tell you after my nap.
No way!
I donít think so.
Without a doubt, no.
The answer is clearly NO.

Solutions

Expert Solution

Here is the python code with comments:

# import the random module so that we can use the choice function
import random
# The function to get the list of responses from the file
def function():
    stream = open('8_ball_responses.txt', 'rt')
    # we delimit the responses with \n
    listofresponses = stream.read().split('\n')
    # now close the stream
    stream.close()
    return listofresponses

listofresponses = function()
# You can uncomment the below 2 lines to see the list of responses
# for i in listofresponses:
#     print(i)

while True:
    print('Ask a the magic ball a question (press -1 to exit) :')
    # convert the input to a string
    userinput = str(input())
    # if the input is -1 then break
    if(userinput == '-1'):
        break
    # use random.choice to randomly pick a list element
    print('The magic ball says: ' + random.choice(listofresponses))

Here is code image to understand the indentation:

Here is the output:


Related Solutions

Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so,” “Yes, of course!,” “I’m not sure,” and so forth. The program should read the responses from the file into an array or ArrayList object. It should prompt the...
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the...
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the user places the ball face down, asks a yes-or-no question and turns the ball face up to reveal the answer. The standard magic 8 ball has 20 standard answers shown below, where 10 are positive (green), 5 are non-committal (yellow), and 5 are negative (red) (credit: Wikipedia) Write a program that does the following: 1. Stores all the responses in a String array using...
1. The demand curve for the Magic 8-Ball toy is P = 15 – 0.5Q. Frances...
1. The demand curve for the Magic 8-Ball toy is P = 15 – 0.5Q. Frances currently has a patent on the concept of predictive billiards accessories, and is thus the only person able to sell Magic 8-Balls. For now, Frances is a monopoly. Her costs are C(QF) = 2QF a. Calculate the (monopoly) market price, quantity produced, and Frances’s profit.   b. Frances’s patent is about to expire, and another producer (Simon) is planning on entering the market. Simon has...
-create a magic 8 ball program in JavaScript - use a loop to ask for the...
-create a magic 8 ball program in JavaScript - use a loop to ask for the question - use a random number to get the answer let randAnswer=Math.round(Math.round()*10); - must have at least 10 different answers -Must use either elseif or switch statement for answer -must output both answer and user input to console - the program should repeat indefinitely until either blank input, or cancel is selected Bug in jsbin.com Please don't use HTML thanks
-create a magic 8 ball program in Javascript. -use a loop to ask for the question...
-create a magic 8 ball program in Javascript. -use a loop to ask for the question -use a random number to get the answer let randAnswer = Math.round(Math.random()*10); -must have at least10 different answers -must use elseif or switch statement -must outputs both answers and user to input the console -program should repeat indefinitely until either blank input or cancel is selected
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number...
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number and then use a switch statement to display the message. Use this website as a resource on possible answers a Magic Eight Ball gives: https://en.wikipedia.org/wiki/Magic_8-Ball
6-Write a module in pseudocode called magicSix(), which accepts two integers and displays a message “Magic...
6-Write a module in pseudocode called magicSix(), which accepts two integers and displays a message “Magic 6!” if either of the two integers is a 6 or if their sum or difference is a 6. Otherwise, the program will display “Not a magic 6.” Note, you will need to determine the larger number when calculating the difference, to get a positive difference. You cannot use any built-in Python functions to do this. Type your pseudocode into your answer document. 7-...
Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
Write a Fortran program which simulates placing 100 molecules into the boxes of a 20 by...
Write a Fortran program which simulates placing 100 molecules into the boxes of a 20 by 20 square grid. Each box can hold at most one molecule. Your program should count and report how many molecules in the final arrangement have no neighbors. Two molecules are considered neighbors if they are directly above or below or directly side by side (diagonals don't count). For instance, if molecules were placed at the locations labelled by letters in the following 5 by...
For this lab you will implement the class Ball which represents a toy ball. Ball should...
For this lab you will implement the class Ball which represents a toy ball. Ball should have a method bounce() whose return type is void, and should have an integer member bounces that counts the number of times the ball has been bounced. Ball should also have a static method named getTotalBounces which counts the total number of times that any Ball object has been bounced. To do this, you will want to include a static integer member totalBounces which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT