基于Python实现的恋爱对话小程序详解
简介
本文讲解如何使用Python编写一个简单的恋爱对话小程序,用户可以随意选择角色性别,进行简单的对话交流。
准备工作
首先,你需要安装Python环境,推荐使用Python 3.6及以上版本。其次,你需要安装几个必要的模块,包括random
和time
。
import random
import time
编写代码
创建男性角色类
首先,我们需要创建一个男性角色类,其中包含了男性角色的基本信息,如姓名,年龄,星座等。
class Man:
def __init__(self, name, age, constellation):
self.name = name
self.age = age
self.constellation = constellation
创建女性角色类
接着,我们需要创建一个女性角色类,其中包含了女性角色的基本信息,如姓名,年龄,星座等。
class Woman:
def __init__(self, name, age, constellation):
self.name = name
self.age = age
self.constellation = constellation
创建对话类
然后,我们需要创建一个对话类,该类包括了对话的基本信息,如对白内容,对白人物等。
class Dialogue:
def __init__(self, content, speaker):
self.content = content
self.speaker = speaker
创建恋爱对话小程序类
最后,我们需要创建恋爱对话小程序类,该类包括了小程序的基本信息,如男性角色,女性角色等。
class LoveDialogue:
def __init__(self, man, woman):
self.man = man
self.woman = woman
self.dialogues = []
def start(self):
self.intro()
while True:
self.show_dialogue_menu()
choice = input().strip()
if choice == '0':
print("Goodbye!")
break
if choice == '1':
self.add_dialogue()
continue
if choice == '2':
self.read_dialogues()
continue
if choice == '3':
self.analyse_dialogues()
continue
def intro(self):
print("Welcome to Love Dialogue!")
print("Your character is {0}, and your partner is {1}".format(self.man.name, self.woman.name))
print("Let's start our dialogue!\n")
def show_dialogue_menu(self):
print("What do you want to do?")
print("1. Add dialogue")
print("2. Read dialogues")
print("3. Analyse dialogues")
print("0. Quit")
def add_dialogue(self):
print("\nType your dialogue:")
content = input().strip()
speaker = random.choice([self.man.name, self.woman.name])
dialogue = Dialogue(content, speaker)
self.dialogues.append(dialogue)
def read_dialogues(self):
for dialogue in self.dialogues:
print("{0}: {1}".format(dialogue.speaker, dialogue.content))
time.sleep(1)
def analyse_dialogues(self):
man_dialogues = []
woman_dialogues = []
for dialogue in self.dialogues:
if dialogue.speaker == self.man.name:
man_dialogues.append(dialogue.content)
else:
woman_dialogues.append(dialogue.content)
print("Man's dialogues:")
for dialogue in man_dialogues:
print("- {0}".format(dialogue))
print("\nWoman's dialogues:")
for dialogue in woman_dialogues:
print("- {0}".format(dialogue))
示例
示例一
男人名叫Jerry,年龄 23,星座处女座;女人名叫Lucy,年龄 21,星座金牛座。他们在一个夏天里相遇了,并且彼此相爱了。以下是他们的对话。
man = Man("Jerry", 23, "Virgo")
woman = Woman("Lucy", 21, "Taurus")
love_dialogue = LoveDialogue(man, woman)
love_dialogue.start()
Welcome to Love Dialogue!
Your character is Jerry, and your partner is Lucy
Let's start our dialogue!
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
1
Type your dialogue:
Hello Lucy, how are you today?
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
1
Type your dialogue:
I'm fine, thank you. How about you Jerry?
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
2
Jerry: Hello Lucy, how are you today?
Lucy: I'm fine, thank you. How about you Jerry?
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
3
Man's dialogues:
- Hello Lucy, how are you today?
Woman's dialogues:
- I'm fine, thank you. How about you Jerry?
示例二
男人名叫Tom,年龄 25,星座双鱼座;女人名叫Kate,年龄 27,星座天秤座。他们是一对恋人,在一起已经两年了。以下是他们的对话。
man = Man("Tom", 25, "Pisces")
woman = Woman("Kate", 27, "Libra")
love_dialogue = LoveDialogue(man, woman)
love_dialogue.start()
Welcome to Love Dialogue!
Your character is Tom, and your partner is Kate
Let's start our dialogue!
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
1
Type your dialogue:
Kate, I love you more and more every day.
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
1
Type your dialogue:
I love you too, Tom. I cannot imagine how life would be without you.
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
2
Tom: Kate, I love you more and more every day.
Kate: I love you too, Tom. I cannot imagine how life would be without you.
What do you want to do?
1. Add dialogue
2. Read dialogues
3. Analyse dialogues
0. Quit
3
Man's dialogues:
- Kate, I love you more and more every day.
Woman's dialogues:
- I love you too, Tom. I cannot imagine how life would be without you.
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于Python实现的恋爱对话小程序详解 - Python技术站