python-checkio/Incinerator/dialogues.py

61 lines
1.6 KiB
Python

VOWELS = "aeiou"
class User:
def __init__(self, name):
self.name = name
self.chat = None
def connect_on_chat(self, o_chat):
self.chat = o_chat
def send(self, text):
self.chat.human_chat += "{} said: {}\n".format(self.name, text)
robot_text = text.lower()
robot_text = "".join(["0" if x in VOWELS else "1" for x in robot_text])
self.chat.robot_chat += "{} said: {}\n".format(self.name, robot_text)
class Human(User):
pass
class Robot(User):
pass
class Chat:
def __init__(self):
self.human_chat = ""
self.robot_chat = ""
def connect_human(self, human: Human):
human.connect_on_chat(self)
def show_human_dialogue(self) -> str:
return self.human_chat.strip('\n')
def connect_robot(self, robot: Robot):
robot.connect_on_chat(self)
def show_robot_dialogue(self) -> str:
return self.robot_chat.strip('\n')
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
chat = Chat()
karl = Human("Karl")
bot = Robot("R2D2")
chat.connect_human(karl)
chat.connect_robot(bot)
karl.send("Hi! What's new?")
bot.send("Hello, human. Could we speak later about it?")
assert chat.show_human_dialogue() == """Karl said: Hi! What's new?
R2D2 said: Hello, human. Could we speak later about it?"""
assert chat.show_robot_dialogue() == """Karl said: 101111011111011
R2D2 said: 10110111010111100111101110011101011010011011"""
print("Coding complete? Let's try tests!")