def add_character(self, name="", char_type="", health="", brawl="", shoot="", dodge="", might="", finesse="", cunning="", **abilities): """ Adds a new character to the league """ # Preconditions of this method: valid data has been entered by the # user: the class and ability names and dice type strings. # Need to check whether a leader has been added to the league. If not # then unless the new character is the leader, then the user should # not be able to add the character to the league: try: if not self.check_leader(char_type): return except CharacterException as e: print(e) return # Need to check that the user has not created a character with # the same name as an existing character: These 'if not' statements # are saying if the result is False then ... if not self.check_duplicate_name(name): # This is the same as returning None return # Check that the user has not attempted to add a character that breaks # the character member rules - MS # May only have one leader. # May only have one sidekick unless 'Company of Heroes' perk is # chosen try: self.check_duplicate_type(char_type) except CharacterException as e: print(e) return # ***Check that adding the character does not exceed the number of # slots remaining for the league - MS # If no errors have been found then the characters can be created new_character = "" try: for subChar in Character.__subclasses__(): if char_type == subChar.__name__: new_character = subChar(self, name, health, brawl, shoot, dodge, might, finesse, cunning, **abilities) if self._max_points < new_character.get_size(): raise CharacterException("There are not enough league points " "left to add " + new_character.get_name() + " the " + new_character.__class__.__name__ + " to the league.") else: self._max_points -= new_character.get_size() print("Character creation of " + name + " the " + char_type + " has been successful") self._all_my_characters.append(new_character) print("League points remaining: " + str(self._max_points)) return new_character except CharacterException as e: print(e.value) del new_character
def check_valid_class(self, input_class): for charac in Character.__subclasses__(): if input_class == charac.__name__: return raise InputException("Invalid class name entry. Please try again")