Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question. Write Pseudocode for the following code. Simulate a six-sided dice roll. Usage: $ python dice.py How many dice do you want to roll? [1-6]

Question.

Write Pseudocode for the following code.

"""Simulate a six-sided dice roll.

Usage:

$ python dice.py How many dice do you want to roll? [1-6] 5

~~~~~~~~~~~~~~~~~~~~~~~~~ RESULTS ~~~~~~~~~~~~~~~~~~~~~~~~~ """

import random DICE_ART = { 1: ( "", " ", " ", " ", "", ), 2: ( "", " ", " ", " ", "", ), 3: ( "", " ", " ", " ", "", ), 4: ( "", " ", " ", " ", "", ), 5: ( "", " ", " ", " ", "", ), 6: ( "", " ", " ", " ", "", ), } DIE_HEIGHT = len(DICE_ART[1]) DIE_WIDTH = len(DICE_ART[1][0]) DIE_FACE_SEPARATOR = " "

def parse_input(input_string): """Return `input_string` as an integer between 1 and 6.

Check if `input_string` is an integer number between 1 and 6. If so, return an integer with the same value. Otherwise, tell the user to enter a valid number and quit the program. """ if input_string.strip() in {"1", "2", "3", "4", "5", "6"}: return int(input_string) else: print("Please enter a number from 1 to 6.") raise SystemExit(1)

def roll_dice(num_dice): """Return a list of integers with length `num_dice`.

Each integer in the returned list is a random number between 1 and 6, inclusive. """ roll_results = [] for _ in range(num_dice): roll = random.randint(1, 6) roll_results.append(roll) return roll_results

def generate_dice_faces_diagram(dice_values): """Return an ASCII diagram of dice faces from `dice_values`.

The string returned contains an ASCII representation of each die. For example, if `dice_values = [4, 1, 3, 2]` then the string returned looks like this:

~~~~~~~~~~~~~~~~~~~ RESULTS ~~~~~~~~~~~~~~~~~~~ """ dice_faces = _get_dice_faces(dice_values) dice_faces_rows = _generate_dice_faces_rows(dice_faces)

# Generate header with the word "RESULTS" centered width = len(dice_faces_rows[0]) diagram_header = " RESULTS ".center(width, "~")

dice_faces_diagram = " ".join([diagram_header] + dice_faces_rows) return dice_faces_diagram

def _get_dice_faces(dice_values): dice_faces = [] for value in dice_values: dice_faces.append(DICE_ART[value]) return dice_faces

def _generate_dice_faces_rows(dice_faces): dice_faces_rows = [] for row_idx in range(DIE_HEIGHT): row_components = [] for die in dice_faces: row_components.append(die[row_idx]) row_string = DIE_FACE_SEPARATOR.join(row_components) dice_faces_rows.append(row_string) return dice_faces_rows

# ~~~ App's main code block ~~~ num_dice_input = input("How many dice do you want to roll? [1-6]: ") # 1. Get and validate user's input num_dice = parse_input(num_dice_input) for i in range(3): # 2. Generates a loop for the number of simulations needed roll_results = roll_dice(num_dice) # 3. Roll the dice dice_face_diagram = generate_dice_faces_diagram(roll_results) # 4. Generate the ASCII diagram of dice faces ####### print(f" {dice_face_diagram}") # 5. Display the diagram print("This is the end of the demonstration") # 6. End of program prompt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data 29th British National Conference On Databases Bncod 2013 Oxford Uk July 2013 Proceedings Lncs 7968

Authors: Dan Olteanu ,Georg Gottlob ,Christian Schallhart

2013th Edition

3642394663, 978-3642394669

More Books

Students also viewed these Databases questions

Question

How did you feel about taking piano lessons as a child? (general)

Answered: 1 week ago