Test

Last updated: May 20th, 20202020-05-20Project preview
In [1]:
import random
In [2]:
def dice():
    return random.randint(1,6)
In [3]:
dice()
Out[3]:
2
In [4]:
def attack(number_of_dice):
    return sum([dice() for _ in range(number_of_dice)])
In [5]:
attack(3)
Out[5]:
9
In [6]:
def battle(number_of_dice,enemy_armour):
    '''Returns True if you beat the enemy and False otherwise'''
    return attack(number_of_dice)>enemy_armour
In [7]:
def two_heads_coin():
    return True
In [8]:
def two_tails_coin():
    return False
In [9]:
def battle_1():
    return battle(3,12)
In [10]:
def battle_2():
    return battle(2,8)
In [11]:
def empiric_prob(experiment,n=10000):
    return sum([experiment() for _ in range(n)])/n
In [12]:
empiric_prob(battle_1,1000000)
Out[12]:
0.259209
In [163]:
empiric_prob(two_tails_coin)
Out[163]:
0.0
In [ ]:
 
Notebooks AI
Notebooks AI Profile20060