Write a program using an object
Start to create your object-oriented text-based adventure game
Extend other people’s classes
Create an enemy in the room
Recap Week 3
Extending your knowledge of OOP
Finish your game
End of the course
In the previous step, you examined the Character class and I asked you to:
Call the method that sets the conversation attribute of a Character object
Call a different method that talks to Dave
Below is the solution I created to the tasks.
The set_conversation method below sets the conversation attribute in the Character class:
class Character():
...
def set_conversation(self, conversation):
self.conversation = conversation
This set_conversation method expects me to pass the message that Dave is going to output:
dave.set_conversation("What's up, dude?")
The Character class contains a talk method that will display a line of dialogue.
class Character():
...
def talk(self):
if self.conversation is not None:
print("[" + self.name + " says]: " + self.conversation)
else:
print(self.name + " doesn't want to talk to you")
The talk method includes a conditional statement to identify whether the conversation attribute has been set.
If the conversation attribute has been set, "[" + self.name + " says]: " + self.conversation will be printed. If not, self.name + " doesn't want to talk to you" will be printed.
Finally, I called the talk method to see my character, Dave, speak.
dave.talk()
You can see my solution in this Trinket.
What choices did you make when creating your code?
How did it compare to your own?
Did you experience any problems completing this challenge?