(相关资料图)

"""需求分析:1.运用面向对象的方式编程2.定义一个心理测试类3.将问题封装在提出问题的方法内4.将答案封装在答案的方法内"""class PsychologicalTest(object):    def __init__(self):        """初始化"""        # 姓名        self.name = input("请输入您的姓名:")        # 性别        self.sex = input("性别是(男或女):")        if self.sex == "男":            self.sex = "大侠"        elif self.sex == "女":            self.sex = "女士"        else:            self.sex = ""        print(f"{self.name}{self.sex}欢迎你来玩心理小游戏o* ̄︶ ̄*o")        print("现在游戏开始...")    def question_1(self):        action_select_1 = input("上战场前三分钟你会做什么?\n"                                "1.打电话 2.什么都不做 3.报仇 4.好好吃一顿\n"                                "选择(如1):")        if action_select_1 == "1":            print("看似活泼的你,其实内心想要找到真爱稳定下来")        elif action_select_1 == "2":            print("对方对你再好,你都会忍耐下来")        elif action_select_1 == "3":            print("你很矛盾,你喜欢的不喜欢你,你不喜欢的却和你过一生")        elif action_select_1 == "4":            print("工作的成就感会让你非常满足,闲暇时你才会想起恋爱")                def question_2(self):        action_select_2 = input("一名警察与挟持人质的歹徒对峙,一声枪响后你认为是什么情况?\n"                                "1.歹徒被击毙 2.歹徒被打伤 3.警察被击毙 4.警察被打伤\n"                                "5.警察误杀人质 6.警察误伤人质 7.歹徒打死人质 8.歹徒打伤人质\n"                                "选择(如1):")        if action_select_2 == "1":            print("独立、坚强")        elif action_select_2 == "2":            print("自信、善良")        elif action_select_2 == "3":            print("自私、固执")        elif action_select_2 == "4":            print("忧郁、内向")        elif action_select_2 == "5":            print("盲目、冲动")        elif action_select_2 == "6":            print("幽默、外向")        elif action_select_2 == "7":            print("倔强、自大")        elif action_select_2 == "8":            print("懦弱、虚伪")    def question_3(self):        action_select_3 = input("朋友把东西忘在你家了,你会怎么做?\n"                                "1.立刻给朋友去 2.约其见面交还东西"                                "3.托人带给朋友 4.以后还\n"                                "选择(如1):")        if action_select_3 == "1":            print("你大胆而冷静,凡事为整体利益为重,不会被小利诱惑")        elif action_select_3 == "2":            print("你态度积极,头脑灵活,工作能力强")        elif action_select_3 == "3":            print("你是乐天派,喜欢帮助他人,不会说拒绝")        elif action_select_3 == "4":            print("你小心谨慎,不鲁莽行事,有强烈责任感")    def question_4(self):        action_select_4 = input("面对一盘蔬果沙拉,你会先吃什么?\n"                                "1.盘边装饰的樱桃 2.蛋白"                                "3.黄瓜 4.沙拉\n"                                "选择(如1):")        if action_select_4 == "1":            print("生活充满活力,你总会寻找种种有趣的细节让自己快乐,并感染他人")        elif action_select_4 == "2":            print("你经常感到疲惫无力,生活没有乐趣,长期的亚健康对你不利")        elif action_select_4 == "3":            print("反反复复的生活使你感到工作,休息,娱乐都像例行公事,你希望有所改变")        elif action_select_4 == "4":            print("你的生活平淡,试着生活中多加入一些小插曲,一点点的改变都会让自己活力满满")    def question_5(self):        action_select_5 = input("如果可以选择梦境,你最想体验哪种梦境?\n"                                "1.风光没好的缠绵梦 2.天天中大奖发大财"                                "3.能治理国家的统帅 4.能成为巨星的梦\n"                                "选择(如1):")        if action_select_5 == "1":            print("你最好的角色是好爸妈,你天生有强烈的父爱/母爱")        elif action_select_5 == "2":            print("你最好的角色是好女儿,你非常孝顺,认为父母人生中第一")        elif action_select_5 == "3":            print("你最好的角色是朋友,重义气,能帮则帮,绝不推辞")        elif action_select_5 == "4":            print("你最好的角色是情人,在工作上一板一眼的你,一旦恋爱,眼里全是对方")    def question_6(self):        action_select_6 = input("你真正想找的恋爱对象?\n"                                "1.咖啡 2.热巧克力奶 3.牛奶 4.茶水\n"                                "选择(如1):")        if action_select_6 == "1":            print("你要的是纯洁的爱情,希望对方与你一切尽在不言中")        elif action_select_6 == "2":            print("你所追求的恋爱对象是那种能领到你的人")        elif action_select_6 == "3":            print("心灵契合是你所需要的,你渴望,憧憬的是一个共同努力的伴侣")        elif action_select_6 == "4":            print("你需要的自由生活空间和时间,不要一个缠着你不放的恋人")    def menu(self):        print("=" * 80)  # 分割线        # 菜单项        print("1.爱情定性测试")        print("2.性格测试")        print("3.形象测试")        print("4.心灵测试")        print("5.人生角色测试")        print("6.恋爱对象测试")        # 空一行        print("")                print("0.退出程序")                action_str = input("请输入菜单编号,进行游戏(如1):")        if action_str == "1":            self.question_1()        elif action_str == "2":            self.question_2()        elif action_str == "3":            self.question_3()        elif action_str == "4":            self.question_4()        elif action_str == "5":            self.question_5()        elif action_str == "6":            self.question_6()        elif action_str == "0":            PsychologicalTest.game_over()           else:            print("请重新输入正确的编号进行游戏!")                            print("=" * 80)  # 分割线    @staticmethod    def game_over():        print("退出游戏,欢迎再来呀!^_^")        exit()    def start_game(self):        while True:            self.menu()if __name__ == "__main__":    psychologicaltest = PsychologicalTest()    psychologicaltest.start_game()

推荐内容