栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python成语接龙人人对战

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python成语接龙人人对战

这是一个可以实现人人对战的函数代码,可以在你写的成语接龙程序中引用这个函数

函数如下:

def idiom_pp(start=0, mode=0, opt=0):      #人人对战
    memory = set()                          # 记忆集合,用于判断成语是否被重复使用
    '''start = 0表示p1先手,start=1表示p2先手,返回值为0表示p1失败,为1表示p2失败'''
    while True:
        p1 = input("p1请输入成语:")
        if p1.strip() == '':
            print("游戏结束!p1输了")
            return 0
        if idiom_exists(p1) == False:
            print("游戏结束,该成语不存在")
            return 0
        p2 = input("p2请输入成语:")
        if p2.strip() == '':
            print("游戏结束!p2输了")
            return 1
        if idiom_exists(p2) == False:
            print("游戏结束,该成语不存在")
            return 1
        memory.add(p2)
        cycle_flag = 0  # 控制while True循环次数
        while True:
            p1 = idiom_select(p2, mode, opt)
            cycle_flag += 1
            if p1 not in memory:
                break
            if cycle_flag == 10:
                p1 = None
                break
            if p1 == None:
                print("恭喜p2,p2赢了!")
                return 1
            else:
                print(p1)
                memory.add(p1)
    while True:
        p1 = input("p1请输入成语:")
        if p1.strip() == '':
            print("游戏结束!p1输了")
        if idiom_exists(p1) == False:
            print("游戏结束,该成语不存在")
            return 0
        if p1 in memory:
            print("游戏结束!该成语已经使用过")
            return 0
        if idiom_test(p1, p2, mode, opt) == False:
            print("游戏结束,你未遵循游戏规则")
            return 0
        memeory.add(p2)
        cycle_flag = 0

    while True:
        p1 = idiom_select(p2, mode, opt)
        cycle_flag += 1
        if p1 not in memory:
            break
        if cycle_flag == 10:
            p1 = None
            break
    if p1 == None:
        print("恭喜p2,p2赢了!")
        return 1
    else:
        print(p1)
        memory.add(p1)

哪里不会的可以问我,可以一起探讨。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号