基于Python实现的恋爱对话小程序详解

yizhihongxing

基于Python实现的恋爱对话小程序详解

简介

本文讲解如何使用Python编写一个简单的恋爱对话小程序,用户可以随意选择角色性别,进行简单的对话交流。

准备工作

首先,你需要安装Python环境,推荐使用Python 3.6及以上版本。其次,你需要安装几个必要的模块,包括randomtime

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技术站

(0)
上一篇 2023年5月23日
下一篇 2023年5月23日

相关文章

  • 如何在Python中使用MongoDB数据库?

    以下是在Python中使用MongoDB数据库的完整使用攻略。 使用MongoDB数据库的前提条件 在使用Python连接MongoDB数据库之前,确保已经安装了MongoDB数据库,并已经创建使用数据库和集合,同时需要安Python的驱动程序,例如pymongo。 步骤1:导入模块 在Python中使用pymongo模块连接MongoDB数据库。以下是导入…

    python 2023年5月12日
    00
  • pip报错“ValueError: invalid literal for int() with base 10: ‘3.3’”怎么处理?

    原因 “ValueError: invalid literal for int() with base 10: ‘3.3’” 错误通常是以下原因引起的: 版本号格式错误:如果您的版本号格式不正确,则可能会出现此错误。在这种情况下,您需要检查版本号格式是否正确。 版本号包含非数字字符:如果您的版本号包含非数字字符,则可能会出现此错误。在这种情况下,您需要删除版…

    python 2023年5月4日
    00
  • python中not、and和or的优先级与详细用法介绍

    以下是详细讲解“Python中not、and和or的优先级与详细用法介绍”的完整攻略,包含两个示例说明。 1. 优先级 在Python中,not、and和or的优先级如下: not and or 这意味着not的优先级最高,or的优先级最低。当表达式中同时包含not、and和or时,not会先被计算,然后是and,最后是or。 为了避免优先级问题,我们可以使…

    python 2023年5月14日
    00
  • python实现键盘输入的实操方法

    当使用Python进行编程时,经常需要获取用户从键盘输入的数据。对于实现键盘输入,有以下几种实现方式: 1. 使用input函数 Python语言内置了一个input函数,用于获取从键盘上输入的数据。具体实现方式如下: name = input("请输入您的姓名:") print("欢迎您," + name) 在上面的…

    python 2023年5月19日
    00
  • Python 获得像素和颜色

    Python 通过Pillow库可以方便地获得图片的像素和颜色信息。下面就会详细讲解如何实现这个过程。 安装Pillow库 要想使用Pillow库,首先需要安装它。可以通过下面的命令在命令行中安装Pillow库: pip install Pillow 打开图片文件 使用Pillow库中的Image模块,可以打开并且载入图片文件: from PIL impor…

    python-answer 2023年3月25日
    00
  • 如何在Python中提取与fft值相关的频率

    要在Python中提取与FFT值相关的频率,需要借助NumPy和SciPy这两个常用的科学计算库。 下面是详细的步骤和示例说明: 步骤一:生成信号数据 首先我们需要生成一个信号数据,作为后续FFT分析的输入。可以使用NumPy库中的fft模块中提供的fftfreq方法来生成一个符合条件的信号数据。 import numpy as np # 生成一个长度为 N…

    python-answer 2023年3月25日
    00
  • Python中用startswith()函数判断字符串开头的教程

    下面是关于Python中用startswith()函数判断字符串开头的完整攻略。 标题:Python 中用 startswith() 函数判断字符串开头 一、什么是startswith()函数 startswith() 函数是Python字符串中的一种内置函数,用于检查字符串是否以特定字符或子字符串开头。 二、startswith()函数的语法 下面是sta…

    python 2023年6月5日
    00
  • python中的多cpu并行编程

    针对题目要求,我为您详细讲解一下 Python 中的多 CPU 并行编程的完整攻略。 什么是多 CPU 并行编程 多 CPU 并行编程是指利用多个 CPU 同时进行任务处理,以提高程序的执行效率和速度。在 Python 中,多 CPU 并行编程多利用多进程或多线程实现,具体方式可以根据不同场景选择不同的模块或库。 多进程并行编程示例 以下是一个用 multi…

    python 2023年5月19日
    00
合作推广
合作推广
分享本页
返回顶部