Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Dice:
- def __init__(self):
- self.__value = random.randint(1,6)
- def roll(self):
- self.__value = random.randint(1,6)
- def __str__(self):
- switcher = {1: "one", 2: "two", 3: "three", 4: "four",
- 5: "five", 6: "six"}
- return ("Next roll: " + switcher[self.__value])
- def main():
- dice1 = Dice()
- dice2 = Dice()
- print(dice1)
- print(dice2)
- dice1.roll()
- dice2.roll()
- print(dice1)
- print(dice2)
- # The following gives AttributeError: 'Dice' object has no attribute '__value'
- # underscores __ creates private attribute by name mangling
- if (dice1.__value == dice2.__value):
- print("DOUBLES")
- main()
Advertisement
Add Comment
Please, Sign In to add comment