From e2851b5cb8dbb6c94089eefb3f1d14cb81d1712c Mon Sep 17 00:00:00 2001 From: ashaypatil12 <90557436+ashaypatil12@users.noreply.github.com> Date: Sat, 8 Jan 2022 10:28:37 +1100 Subject: [PATCH] Added scenario handling for invalid input 1. In case the user has not entered valid input, give them an option to try again. 2. Used docstring instead of multiple prints 3. Used recursive function --- Dice-Simulator/dice.py | 113 ++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 52 deletions(-) diff --git a/Dice-Simulator/dice.py b/Dice-Simulator/dice.py index 798e30dd..c7a7f62f 100644 --- a/Dice-Simulator/dice.py +++ b/Dice-Simulator/dice.py @@ -1,62 +1,71 @@ -import random # also do with numpy (from numpy import random) +import random -# ------------ function definition +def roll_dice(): # ------------ function definition + choice = input("Do you want to continue (y/n) :") + if choice.lower() == 'y': + number = random.randint(1, 6) + if number == 1: + print(""" # instead of multiple prints, use one print docstrings + ----------- + | | + | 0 | + | | + ----------- + """) -def roll_dice(): - number = random.randint(1,6) - if number == 1: - print("-----------") - print("| |") - print("| 0 |") - print("| |") - print("-----------") + elif number == 2: + print(""" + ----------- + | | + | 0 0 | + | | + ----------- + """) - elif number == 2: - print("-----------") - print("| |") - print("| 0 0 |") - print("| |") - print("-----------") + elif number == 3: + print(""" + ----------- + | 0 | + | 0 | + | 0 | + ----------- + """) - elif number == 3: - print("-----------") - print("| 0 |") - print("| 0 |") - print("| 0 |") - print("-----------") + elif number == 4: + print(""" + ----------- + | 0 0 | + | | + | 0 0 | + ----------- + """) - 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) + elif number == 5: + print(""" + ----------- + | 0 0 | + | 0 | + | 0 0 | + ----------- + """) + elif number == 6: + print(""" + ----------- + | 0 0 | + | 0 0 | + | 0 0 | + ----------- + """) + roll_dice() # Call the same function to ask if they want to continue + elif choice.lower() == 'n': + print("Thanks for playing") # terminate the program + else: + print("You entered an Incorrect input. ") + roll_dice() # Incase the input is invalid, Call the same function to ask if they want to continue +print("<-------------------->Dice Simulator<------------------------->") +roll_dice()