Question

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
allMessages = [ 'It is certain',
                'It is decidedly so',
                'Without a doubt',
                'Yes definitely',
                'You may rely on it',
                'As I see it, yes',
                'Most likely',
                'Outlook good',
                'Yes',
                'Signs point to yes',
                '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' ]

eightMessages = random.sample(allMessages, 8)
print(random.sample(eightMessages, 1)[0])
# Most likely

This is wonderful (at one time, a run of the program actually tells me this: "as I see it, yes"). But we don't get any control of the positive or negative answers there. As stated in the linked wikipedia page, the first 10 answers should be positive ones, the next 5 neutral, and the last 5 negative. Now, we want to write a new magic 8 ball function called magic8ball that will randomly pick an answer from 8 samples. But this time the 8 samples consists of 4 random positive, 2 neutral, and 2 negative answers, respectively. Submit your function in the answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE:

import random

def magic8ball():

    # list contains answers
    allMessages = [ 'It is certain',
                'It is decidedly so',
                'Without a doubt',
                'Yes definitely',
                'You may rely on it',
                'As I see it, yes',
                'Most likely',
                'Outlook good',
                'Yes',
                'Signs point to yes',
                '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' ]

    # empty list to store answer
    eightMessages=[]

    # 4 random selection of positive answer from the list
    positive_answer=random.sample(allMessages[:10],4)

    # adding the positive answers to the 'eightMessages' list
    for p in positive_answer:
        eightMessages.append(p)

    # 2 random selection of neutral answer from the list
    neutral_answer= random.sample(allMessages[10:15],2)  

    # adding the neutral answers to the 'eightMessages' list
    for neutral in neutral_answer:
        eightMessages.append(neutral)

    # 2 random selection of negative answer from the list
    negative_answer=random.sample(allMessages[15:],2)

    # adding the neutral answers to the 'eightMessages' list
    for negative in negative_answer:
        eightMessages.append(negative)

    # random selection of 1 answer from the 'eightMessage' list and
    # displayed
    print(random.sample(eightMessages,1)[0])


# testing
if __name__=='__main__':

    # calling the function
    magic8ball()
  


SCREENSHOT FOR CODING:

import random def magic8ball ) # 1ist contains answers [It is certain, It is decidedly so, Without a doubt, Yes definitely

SCREENSHOT FOR OUTPUT:

Reply hazy try again >>> RESTART outlook not so good >> RESTART My sources say no >> RESTART without a doubt >>> RESTART outl

Add a comment
Know the answer?
Add Answer to:
Using python The following is a modified version of the magic 8 ball. We have 20...
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 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...

  • 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...

  • what is wrong with the following code? public class EightBall { private static Scanner scanner =...

    what is wrong with the following code? public class EightBall { private static Scanner scanner = new Scanner(System.in); private static Random rnd = new Random();    public static String getAnswer(int category, int answer) { if (category >= 74) {       if (answer == 0) { return "As I see it, yes." ; } else if (answer == 1) { return "Signs point to yes." ; } else if (answer == 2) { return "Outlook good." ; } else if...

  • ==Modify the M8B by using C#== using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

    ==Modify the M8B by using C#== using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace M8B {     class Magic8Ball     {         static void Main(string[] args)         {             string question;             Console.WriteLine("I am the Magic 8-ball! Ask me a question and I will give you an answer.");             Console.Write("Your question: ");             question = Console.ReadLine();             Console.WriteLine("\nLet me part the mists of the time for you.");             Random rnd = new Random();             int roll =...

  • PROMT: Step 1 - Reading the code This is one of the few labs you will...

    PROMT: Step 1 - Reading the code This is one of the few labs you will only have to write a single method. While you are free to write more methods (hint: it may be easier with more), you are not required to write more. Before you start, take a moment to read the code provided. You will notice that the method missing is: public static String getAnswer(int category, int answer). This is the method you will write. For reference,...

  • 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...

  • Write a Python program that generate randomly a magic square of 3 x 3 elements. For...

    Write a Python program that generate randomly a magic square of 3 x 3 elements. For example: 4 3 8 9 5 1 2 7 6      = Matrix1 Matrix 1 above is an example of magic square. The rows total, the columns total, and the diagonal totals are all 15. Your program should randomly generate a 3x3 matrix. Check if it is a magic square. If it is a magic square, then print some information and quit. If the...

  • 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 <<...

  • Please help as it is very important task for me please write in Python code and...

    Please help as it is very important task for me please write in Python code and divide this into function and write comments for it 1. Ask the player’s name and welcome them to the game using their name. 2. Ask the player what is par for this game (number between 3-5 inclusive) 3. Ask the player what the distance to the hole for this game is (whole number between 195 and 250 inclusive) 4. Show the game menu: (I)nstructions...

  • Page 1 Question 1 Suppose we take repeated random samples of size 20 from a population...

    Page 1 Question 1 Suppose we take repeated random samples of size 20 from a population with a Select all that apply. mean of 60 and a standard deviation of 8. Which of the following statements is 10 points true about the sampling distribution of the sample mean (x)? Check all that apply. A. The distribution is normal regardless of the shape of the population distribution, because the sample size is large enough. B. The distribution will be normal as...

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