Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2023 20:35
Show Gist options
  • Save stephengruppetta/26bcb7aaef7ccf315b77e66ef437c609 to your computer and use it in GitHub Desktop.
Save stephengruppetta/26bcb7aaef7ccf315b77e66ef437c609 to your computer and use it in GitHub Desktop.
import random
class Wizard:
def __init__(self, name, patronus, birth_year): = name
self.patronus = patronus
self.birth_year = birth_year = None
self.wand = None
self.skill = 0.2 # 0.0 (bad) to 1.0 (good)
def increase_skill(self, amount):
self.skill += amount
if self.skill > 1.0:
self.skill = 1.0
def assign_wand(self, wand):
self.wand = wand
print(f"{} has a {self.wand} wand.")
def assign_house(self, house): = house
def cast_spell(self, spell):
if self.wand:
effect = self.wand.cast_spell(spell, self)
if effect:
print(f"{} cast {effect}!")
print(f"{} failed to cast {}!")
print(f"{} has no wand!")
class Professor(Wizard):
def __init__(self, name, patronus, birth_year, subject):
super().__init__(name, patronus, birth_year)
self.subject = subject
def assign_wand(self, wand):
def assess_student(self, student, grade):
if self.subject in student.subject_grades:
f"{} assessed {} in "
f"{self.subject}. The grade is {grade}%."
student.take_exam(self.subject, grade)
f"{} doesn't study {self.subject}."
class Student(Wizard):
def __init__(self, name, patronus, birth_year, school_year):
super().__init__(name, patronus, birth_year)
self.school_year = school_year
self.subject_grades = {}
def assign_house_using_sorting_hat(self):
# ToDo: Sorting Hat chooses house and assigns it to student.
# We'll work on this in the final Year
# The parent class has 'assign_house()', which you'll call
# in this method
def take_exam(self, subject, grade):
self.subject_grades[subject] = grade
def assign_subjects(self, subjects):
self.subject_grades = {subject: None for subject in subjects}
class House:
def __init__(self, name, founder, colours, animal): = name
self.founder = founder
self.colours = colours
self.animal = animal
self.members = []
self.points = 0
def add_member(self, member):
if member not in self.members:
def remove_member(self, member):
def update_points(self, points):
self.points += points
def get_house_details(self):
return {
"founder": self.founder,
"colours": self.colours,
"animal": self.animal,
"points": self.points
class Wand:
def __init__(self, wood, core, length, power=0.5):
self.wood = wood
self.core = core
self.length = length
self.power = power # 0.0 (weak) to 1.0 (strong)
def cast_spell(self, spell, wizard):
if spell.is_successful(self, wizard):
return spell.effect
return None # Explicitly return None (for readability)
class Spell:
def __init__(self, name, effect, difficulty): = name
self.effect = effect
self.difficulty = difficulty # 0.0 (easy) to 1.0 (hard)
def is_successful(self, wand, wizard):
success_rate = (
(1 - self.difficulty)
* wand.power
* wizard.skill
return random.random() < success_rate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment