From 0f89cec70426338dc1ba07b769975dab19702bc6 Mon Sep 17 00:00:00 2001 From: rohitgaikwad17 Date: Mon, 11 Aug 2025 12:30:16 +0530 Subject: [PATCH] Update dice.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switched from Python’s built-in random to NumPy’s random.randint() Fixed typo in title (Dics Simulator → Dice Simulator) Replaced multiple if-elif blocks with a dictionary for cleaner, more maintainable dice face printing Updated loop exit logic to use break instead of exit(0) --- Dice-Simulator/dice.py | 114 ++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 58 deletions(-) diff --git a/Dice-Simulator/dice.py b/Dice-Simulator/dice.py index 798e30dd..d02039cd 100644 --- a/Dice-Simulator/dice.py +++ b/Dice-Simulator/dice.py @@ -1,62 +1,60 @@ -import random # also do with numpy (from numpy import random) - - -# ------------ function definition +from numpy import random # Using NumPy random + +# Dice faces stored in a dictionary +dice_faces = { + 1: [ + "-----------", + "| |", + "| 0 |", + "| |", + "-----------" + ], + 2: [ + "-----------", + "| |", + "| 0 0 |", + "| |", + "-----------" + ], + 3: [ + "-----------", + "| 0 |", + "| 0 |", + "| 0 |", + "-----------" + ], + 4: [ + "-----------", + "| 0 0 |", + "| |", + "| 0 0 |", + "-----------" + ], + 5: [ + "-----------", + "| 0 0 |", + "| 0 |", + "| 0 0 |", + "-----------" + ], + 6: [ + "-----------", + "| 0 0 0 |", + "| |", + "| 0 0 0 |", + "-----------" + ] +} def roll_dice(): - number = random.randint(1,6) - if number == 1: - print("-----------") - print("| |") - print("| 0 |") - print("| |") - print("-----------") - - elif number == 2: - print("-----------") - print("| |") - print("| 0 0 |") - print("| |") - print("-----------") - - elif number == 3: - print("-----------") - print("| 0 |") - print("| 0 |") - print("| 0 |") - print("-----------") - - elif number == 4: - print("-----------") - print("| 0 0 |") - print("| |") - print("| 0 0 |") - print("-----------") - - elif number == 5: - print("-----------") - print("| 0 0 |") - print("| 0 |") - print("| 0 0 |") - print("-----------") - - elif number == 6: - print("-----------") - print("| 0 0 0 |") - print("| |") - print("| 0 0 0 |") - print("-----------") - - -print(" Dics Simulator ") -x = 'y' -while x.lower() == "y": - roll_dice() # function call - choice = input("Do you want to play again (y/n): ") # choice from user - - if choice.lower() == "n": - exit(0) - - + number = random.randint(1, 7) # 1 to 6 (upper bound exclusive) + for line in dice_faces[number]: + print(line) +print(" Dice Simulator ") +while True: + roll_dice() + choice = input("Do you want to play again (y/n): ").lower() + if choice == "n": + break