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

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

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

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

  • C++. Need some help getting started. We will also have the following two functions: 1. A...

    C++. Need some help getting started. We will also have the following two functions: 1. A mutate function that randomly modifies a chromosome. 2. A crossover function that takes two chromosomes and splits each one at the same spot, then combines them together. Our genetic algorithm works by iterating over generations of chromosomes via the following process: 1. Generate random population. 2. Until we get an answer that is good enough, do the next steps in a loop: (a) Do...

  • In this assignment, you will explore more on text analysis and an elementary version of sentiment...

    In this assignment, you will explore more on text analysis and an elementary version of sentiment analysis. Sentiment analysis is the process of using a computer program to identify and categorise opinions in a piece of text in order to determine the writer’s attitude towards a particular topic (e.g., news, product, service etc.). The sentiment can be expressed as positive, negative or neutral. Create a Python file called a5.py that will perform text analysis on some text files. You can...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words 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
ADVERTISEMENT