Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Hello! I am currently learning more in-depth about Python. I have some code that I have made progress on but I am unsure of how to proceed. It is a magic 8-ball program with twelve set responses that I have in the first part of the code. I want to add a while loop, a for loop, and an if statement but I am unsure how to proceed. At the end, I also want the user to be able to play again after entering their question and getting a response, while keeping track of how many times they have played and displaying the value once they are done playing. I also want the end output to have the question that the user asked, the response that was randomly selected and asterisks to surround the output so that it looks neat.

What is the best way to do this? Here is my current code:

import random

# List of magic 8 ball responses responses = [
"Yes, absolutely!",
"Without a doubt, of course, yes.",
"You can count on it!",
"For sure!",
"Hmmm...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",
"Absolutely not!",
"The answer is clearly NO!"
]

# Starting function to get a random response from the program

def get_random_response():
      return random.choice(responses)

# Main function for the magic 8 ball game

def magic_8_ball():
      play_count = 0

Expert Solution
Check Mark
Step 1: Algorithm of the program:

Algorithm:

  1. Define a function get_random_response to get a random response from the list.
  2. Define a function get_random_response to get a random response from the list.
  3. Define a function magic_8_ball:
    a. Initialize play_count to 0.
    b. Start an infinite loop.
    c. Prompt the user for a question.
    d. If the user types 'quit', exit the loop.
    e. Generate a random response using get_random_response.
    f. Increment play_count by 1.
    g. Print the user's question and the magic 8 ball's response surrounded by asterisks.
  4. After the loop, display the total number of questions asked (play_count).
  5. Call the magic_8_ball function to run the program.
Still need help?
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

This is perfect,  thank you! I have one last question:

Is it possible to also add a 'for' loop while modifying the code so it fits the for loop along with the while loop as well? If so, can you edit and give an example of that please? Thank you!

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

This is perfect,  thank you! I have one last question:

Is it possible to also add a 'for' loop while modifying the code so it fits the for loop along with the while loop as well? If so, can you edit and give an example of that please? Thank you!

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education