03 Jun Aurora Saturday 14:30 Python Homework 20.05.30.
Question:
Modify the Die class we made in the last class blow, so it can complete the following tasks:
- You could compare with different dies using > and <. In order to complete this task, you need to overwrite __gt__ and __lt__ methods.
- You could use print() to print the die object show how many sides and current value
import random
class Die:
def __init__(self, sides):
self.sides = sides
self.value = 1
def roll(self):
self.value = random.randint(1, self.sides)
def getValue(self):
return self.value
def __eq__(self, anotherDie):
if isinstance(anotherDie, Die) == True and self.value == anotherDie.value:
return True
elif isinstance(anotherDie, int) == True and self.value == anotherDie:
return True
return False
Sorry, the comment form is closed at this time.