Python Codes The Series - 2
Playing Ludo or Snakes & Ladders and just lost 😰 your dice 🎲; well, no need to worry till I am with you 🙋🏻♂️. Just use Python. Many of you might think 🤔 that I am crazy but believe me I am not crazy 😧. We will learn today to use Python for making a rolling dice simulator program.
Here, we are using random module to use the various random functions to make the probability of each outcomes equal.
Copy the following code:
#importing random module...
import random
'''
Taking a number between 1 and 10000
using randrange function
'''
start = 1
end = 10000
num = random.randrange(start,end+1)
#Now choosing the first digit of num from left...
num_2 = str(num)[0]
num_2 = int(num_2)
'''
Finally, choosing the number for the die
'''
if num_2 < 6:
roll = random.randrange(1,num_2+1)
elif num_2 == 6:
roll = num_2
else:
num_2 = num_2 - 3
roll = random.randrange(1,num_2+1)
print("You got",roll)
input("Press Enter to exit.")
©SGPython
Comments
Post a Comment