UrbanPro
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
Follow 1

Please Enter a comment

Submit

Other Lessons for You

What is M.S.Project ?
MICROSOFT PROJECT contains project work and project groups, schedules and finances.Microsoft Project permits its users to line realistic goals for project groups and customers by making schedules, distributing...

Why is the Hadoop essential?
Capacity to store and process large measures of any information, rapidly. With information volumes and assortments always expanding, particularly from web-based life and the Internet of Things (IoT), that...

Python - Solving Ordinary Differential Equations - Predator Prey Model aka Lotka Volterra Equations
This is an assignment in Python, I contributed to a numerical Python MOOC from George Washington University. The link to this assignment on github is here. The Lotka–Volterra equations, also...
S

Sri Vallabha Deevi

0 0
1

Relational operators
Operation Syntax Function Ordering a < b lt(a, b) Ordering a <= b le(a, b) Equality a == b eq(a, b) Ineqality a != b ne(a, b) Ordering a >= b ge(a, b) Ordering a > b gt(a, b)

PEP (Python Enhancement Proposals)
Python Enhancement Proposals(PEP) are suggestions for improvements to the language, made by experienced Python developers. PEP 8 is a style guide on the subject of writing readable code. It contains a...
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

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more