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:
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:
领英推荐
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")
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:
For the wrong input:
Thank you all :) Hope you find this insightful :)