TORONTO KIDS COMPUTER CLUB | Aurora Saturday 14:30 Python Homework 20.05.30.
17591
post-template-default,single,single-post,postid-17591,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-7.6.2,wpb-js-composer js-comp-ver-6.10.0,vc_responsive

Aurora Saturday 14:30 Python Homework 20.05.30.

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

No Comments

Sorry, the comment form is closed at this time.