I have written a class to simulate the throw of a dice. I am trying to implement the private method __check_dice(self) to catch errors when creating an instance of the class to avoid code duplication:
import numpy as np
class Dice():
""" Simulating the roll of a dice with possible outcomes ranging
from first_num to last_num (inclusive) """
def __init__(self, first_num=1, last_num=6):
self.first_num = first_num
self.last_num = last_num
def __check_dice(self):
if self.last_num >= self.first_num >= 0:
return True
else:
return "The first number should be smaller than the last.." \
"and both positive. I can't create your dice.."
def sides(self):
if __check_dice(self):
return "Our dice has {} sides". \
format(self.last_num + 1 - self.first_num)
def roll(self):
if __check_dice(self):
return "You rolled a " + \
str(np.random.choice(np.arange(self.first_num,
self.last_num + 1)))
dice_1 = Dice(-1, 18)
print(dice_1.sides())
print(dice_1.roll())
The logic is that when the __check_dice evaluates to True, the two following methods can be executed if called. However when running the code I get the following error:
if __check_dice(self):
NameError: name '_Dice__check_dice' is not defined
Why is it not possible to call the __check_dice method within the scope of the class inside another method? I have also tried without making the method private, but I get similar error.