The Dice Rolling Simulator
A dice

The Dice Rolling Simulator

In this article, we are going to form the small and simple project of "Rolling Dice in Python". I will be explaining each line of code step-wise. and So let's start:

Before proceeding to any code proper analysis and imagination must be done to implement it effectively. So here our consideration is:

  1. The dice will give numbers from 1 to 6.
  2. After each roll, the system will ask the user whether to quit or not
  3. If the user denies then the system will give another roll to the dice
  4. If the user agrees then the system will exit with an acknowledgment.

As we know whenever we draw a Dice its output is random i.e. for each of its 6 values the probability is 1/6. So we are going to import a python library naming "random" that will help us to give the random number from the range 1 to 6.

import random        

Now let's create a function where the user wants the system to roll a dice. We are creating a function because this call is made again and again and to make our code efficient.

def usr():
    return random.randint(1,6)        

Let's call for the 1st Roll

print("Dice Number:", usr())        

We are going to make a loop in which our system will be there until the user no longer needs the output so let's create a boolean variable x:

x=True        

Creating the Loop:

while x:        

Now we are in Loop:

  • Since we already printed our output for once now we will ask the user whether to quit or not and storing in a new variable

n = input("Do you want to quit ? (Y/N)\n")        

  • If the answer is yes, we will exit by making while False and Acknowledging the user

if n in ['Y', 'y']:
    print("Bye!!")
    x=False        

  • Else if the user declines then the system will roll dice again

elif n in ['N', 'n']:
    print("Dice Number:", usr())        

  • Another case for if the user entered the wrong input the system will exit like the last 2nd point

else:
    print("Invalid Input")        

End of Loop

(OPTIONAL) Some designing: Inserted in beginning

print("**********************************")
print("             WELCOME")
print("    THE DICE ROLLING SIMULATOR")

print("**********************************")        

The whole code:

print("**********************************")
print("             WELCOME")
print("    THE DICE ROLLING SIMULATOR")
print("**********************************")
import random
def usr():
    return random.randint(1,6)
print("Dice Number:", usr())
x=True
while x:
    usr()
    n = input("Do you want to quit ? (Y/N)\n")
    if n in ['Y', 'y']:
        print("Bye!!")
        x=False
    elif n in ['N', 'n']:
        print("Dice Number:", usr())
    else:
        print("Invalid Input")
	        
        x=False        

Output:

No alt text provided for this image

For the wrong input:

No alt text provided for this image

Thank you all :) Hope you find this insightful :)

要查看或添加评论,请登录

Kartikey Garg的更多文章

  • Crypto Price Prediction Report

    Crypto Price Prediction Report

    Disclaimer This project was developed with the assistance of an AI companion, Microsoft Copilot. The AI generated all…

  • The Growing Generative AI

    The Growing Generative AI

    Warm Greetings to all, let's proceed further without taking any further time in welcoming because as you read this…

  • Encourage employees for Wide Knowledge

    Encourage employees for Wide Knowledge

    Hello, working professionals, I hope you all are having a great day at your job. My name is Kartikey Garg, a fresher…

  • Inputting data in Google Search using Selenium

    Inputting data in Google Search using Selenium

    Hello all, in this article, we will focus on the use of Selenium in searching for any word. Let us begin: 1st of all we…

  • Web login using Selenium

    Web login using Selenium

    In this article, we will study the use of Selenium. We will practice login into Facebook.

  • Magic 8 Ball Game

    Magic 8 Ball Game

    Today we are code for the "Magic 8 ball" game in Python Our Assumptions: A list of answers representing 8 balls The…

  • Python Number Guessing

    Python Number Guessing

    In this article, we are going to make another basic python project on "Number guessing" Our assumptions: A total of 3…

    2 条评论
  • Reversing Time: When Time goes Negative

    Reversing Time: When Time goes Negative

    Hello Everyone, hope all are doing well and great, I was eager to share my own thoughts regarding time travel. Today's…

社区洞察

其他会员也浏览了