true

Learn Python Training from the Best Tutors

• Affordable fees
• 1-1 or Group class
• Flexible Timings
• Verified Tutors

Search in

# Simulation of a die-roll:Usage of random module

Ashish K Sharma
10/06/2022 0 0

Task:Simulation of a simple die-rolling game,where we keep rolling a six-sided die until we have rolled a total of 20.

To simulate a six-sided die roll, we need a way to generate a random integer between 1 and 6. The random module in Python’s standard library provides us with various functions to generate random numbers. The function that we’ll use to write this program is  the randint() function, which generates a random integer in a given range.

The randint() function takes two integers as arguments and returns a random integer that falls between these two numbers (both inclusive). Calling the randint() function allows us to simulate the roll of our
virtual die. Every time we call this function, we’re going to get a number between 1 and 6, just as we would if we were rolling a six-sided die. So,now we write our program:

import os,sys
import random
#Roll a die until the total score is 20

target_score = 20
def roll():
return random.randint(1,6)

score = 0
num_rolls = 0
while (score < target_score):
die_roll = roll()
num_rolls = num_rolls+1
print('Rolled: {0}'.format(die_roll))
score = score + die_roll

print('Score of {0} reached in {1} rolls'.format(score,num_rolls))

Example Output

Rolled: 4
Rolled: 2
Rolled: 6
Rolled: 4
Rolled: 6
Score of 22 reached in 5 rolls

Note:The format() method lets you plug in labels and set it up so that they get
printed out in a nice, readable string with extra formatting around it.

0 Dislike

## Other Lessons for You

DBMS - SQL - Any/All
All - Operator SELECT empno, sal FROM emp WHERE sal > ALL (1999, 2999, 3999); Output of Above query is same as below query SELECT empno, sal FROM emp WHERE sal > 1999 AND sal > 2999...

Multiple Decorators
#Multiple decorators can be chained in Python.#A function can be decorated multiple times with different (or same) decorators.#We simply place the decorators above the desired function. def star(func):...

Python Important Operation Formats and Sytnax.
Hello Python Enthusiast, Though Python is considered as one of the easisest language to learn in the market, many freshers and beginners will find great difficulty in understansing the syntax and also...

Benefits of Design Patterns in Application Development
Application developments is a tedious job. Programmers write code and test it again and again to make sure their software works fine. So designing complex software is a hard nut to crack. Skilled programmers...

How to know if an object is iterable?
An object in python is said to be an iterable if it obeys one of the following two rules:- 1. Object must consist of __getitem__ method 2. Object must consist of __iter__ method. String objects generally...

### Looking for Python Training classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you

X

### Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

• Select the best Tutor
• Book & Attend a Free Demo
• Pay and start Learning

### Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro