Question

In this module you learned about File Handling. You began learning about how data can be...

In this module you learned about File Handling. You began learning about how data can be imported into, manipulated in, and exported from a program. Alter the assignment from Ch6 (Magic 8 Ball Game) so that it reads the Magic 8 Ball game sayings from a file and loads them into an array. The rest of the program should be the same.

Previous Game:

"The magic ball eight was created in the 50's and was produced by mattel. Eight Ball is a toy and is just a game. Using the attached file, you will need to store the sayings in an array. The magic 8 ball answers your yes no questions. Have the player types/asks a question. One of the sayings should be randomly picked from the array and displayed to the player."

Update the pseudocode for the updated program that simulates the Magic eight ball game.

Make a working version of this program in Python.

Complete the pseudocode using notepad. Save your work and upload it to the Blackboard assignment area by clicking on the Browse My Computer button below the text editor. Complete the Python code using IDLE. Upload your .py file to the Blackboard assignment area by clicking on the Browse My Computer button below the text editor. This assignment is worth 20 points and will be evaluated using the objectives of the assignment listed.

EightBData.txt
"As I see it, yes"
"It is certain"
"Most likely"
"Outlook good"
"Without a doubt" 
"Yes - definitely"
"You may rely on it"
"Reply hazy, try again"
"Ask again later"
"Better not tell you now"
"Cannot predict now"
"Concentrate and ask again"
"Don't count on it"
"My reply is no"
"My sources say no"
"Outlook not so good"
"Very doubtful"
"Do I Look Like I Care?"
"Yeah, Right"
"Ask Again Later"
"DUH!"
0 1
Add a comment Improve this question Transcribed image text
Answer #1

The Python3 code for the given question is below.

#importing random module
import random

#open the EightBData.txt file in read mode
fp=open("EightBData.txt", "r")

#read and split each line of the file and append it to a list called array
array=fp.read().split("\n")

#close the file once the operations are over
fp.close()

#read the question as user input
str=input('Enter your question: ')

#print a random item in the list using choice() function of random module
print(random.choice(array))

If the indendations are not clear, please see the attached screenshot of the code.

#importing random module import random #open the EightData.txt file in read mode fp=open(EightBData.txt, r) #read and spl

Sample Output

Enter your question: Will I win the lottery? My reply is no

Explanation

The working of the code is given below.

  • Open the file EightBData.txt in read mode by using a file pointer fp. The method to open a file in Python is open().
  • Read the contents of the file using a method read(). Split each new line of the file using split() method and add it to a list called array.
  • Close the file since all the file operations are over.
  • Read a question as user input via input() method.
  • Using a method choice() of random module, randomly select an item from the list array.
  • Display the array item using print() method.

Hope this helps. Doubts, if any, can be asked in the comment section.

Also do upvote the answer if found helpful.

Add a comment
Know the answer?
Add Answer to:
In this module you learned about File Handling. You began learning about how data can be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this module you learned about arrays. You also began learning about implementing arrays and how...

    In this module you learned about arrays. You also began learning about implementing arrays and how they can help organize the data in a program. The magic ball eight was created in the 50's and was produced by mattel. Eight Ball is a toy and is just a game. The magic 8 ball answers your yes/no questions. You will be creating a version of the Magic 8 Ball game. You can use whats inside the attached file to build your...

  • Using python The following is a modified version of the magic 8 ball. We have 20...

    Using python The following is a modified version of the magic 8 ball. We have 20 mysterious answers to questions about anything in the universe. Each time we start this program by randomly picking 8 answers and then we randomly pick 1 from these 8 answers. This lucky answer will then be printed. This time we use the sample() function from the random library. This function randomly samples a list and return the random samples in a list. import random...

  • Can someone please help me with this code? I'm writing in C++. Thank you in advance....

    Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers). In order to complete this, you will need a couple of new functions. First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline: string question; cout <<...

  • c++ can use if else, loop,function ,array,file i.o. Can not use swtich, global variables etc..   Please...

    c++ can use if else, loop,function ,array,file i.o. Can not use swtich, global variables etc..   Please do all parts. previously I post 3 time for part1 ,2 and 3 but I can't make it into one complete program code. I even try to post a questions ask and post the 3 parts of code for experts to put it together. But experts said it's not enough information. please help... Thank you ! Program Objectives: Allow students to create arrays Allow...

  • General Requirements . . . Write a program that reads letters from a file called "letters.txt"....

    General Requirements . . . Write a program that reads letters from a file called "letters.txt". Your program will open the letters.txt file read in one character at a time For this assignment the test file will contain at least 30 letters, uppercase OR lowercase In your program you will change each letter (solution) to uppercase Use the function toupper in #include <ctype.h> You create a numerical version of the uppercase letter from the file . o Use int numberSolution...

  • Have you ever wanted to predict the future? Well the Magic Eight Ball does just that....

    Have you ever wanted to predict the future? Well the Magic Eight Ball does just that. The original game was a softball sized “8-ball”. You would ask a question, shake it up and look at the result. There are 20 responses…10 positive, 5 negative, and 5 are vague. For this project, we want to recreate this, but give the ability to read in a set of responses, and add additional responses, and print out all of the responses in alphabetical...

  • C++ programming Submit assignment to Blackboard by the midnight on the due date listed above. (Verify...

    C++ programming Submit assignment to Blackboard by the midnight on the due date listed above. (Verify if your submission is completed.) The only people you are to discuss this project with are myself and the CS Department tutor. Duplicate code, if discovered, will result in a 0 grade. Your source file(s) should follow the Computer Science Coding Standards (available at the Computer Science Homepage) including proper indenting and spacing 1 Overview program, you will play a game of hangman. In...

  • #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...

  • in c++ please Page 1 of 3 (PRO) Project Assignment Instructions Last Charged: 6/1/2020 Read and...

    in c++ please Page 1 of 3 (PRO) Project Assignment Instructions Last Charged: 6/1/2020 Read and follow the directions below carefully and perform the steps in the order listed. You will be solving one program as instructed and turning in your work electronically via an uploaded file within Eagle Online/Canvas and copy & paste the program to the Text Entry box as well. Make sure and check your work prior to uploading the assignment (NOTE: For Steps 1 & 2...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT