Unit 5 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Implement a setter method set_character()
within the Player
class that updates the character attribute after validating the input against a list of allowed characters.
1) Define a method `set_character` with a parameter `name`.
2) Check if the `name` is within the predefined list of valid characters.
3) If valid, update the `character` attribute and print "Character updated".
4) If not valid, print "Invalid character".
⚠️ Common Mistakes
class Player():
def __init__(self, character, kart):
self.character = character
self.kart = kart
self.items = []
def set_player(self, name):
valid_characters = ["Mario", "Luigi", "Peach", "Yoshi", "Toad", "Wario", "Donkey Kong", "Bowser"]
if name in valid_characters:
self.character = name
print("Character updated")
else:
print("Invalid character")