Q:

Python program to design a dice throw function

belongs to collection: Python basic programs

0

Here, we are going to build a dice() function using python. The program is so simple as an introductory program for defining a function. The function is going to use an inbuilt library naming random. This random library helps us to choose a random value of the variable within the range.

    random.choice([1,2,3,4,5,6])

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Code:

import random

# function definition "dice"
# it will return a random value from 1 to 6
def dice():
    return random.choice([1,2,3,4,5,6])

# main code
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())

Output

DICE THREW :  5  
DICE THREW :  3  
DICE THREW :  5  
DICE THREW :  4  
DICE THREW :  3  
DICE THREW :  5  
DICE THREW :  3  
DICE THREW :  3  
DICE THREW :  4  
DICE THREW :  6 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to design a biased dice throw funct... >>
<< Python program to print perfect numbers from the g...