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 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 C++ program that displays the numbers between 1000 and 9999, which are divisible by...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT