0
点赞
收藏
分享

微信扫一扫

python-纸牌小游戏练习题

mafa1993 2022-04-25 阅读 93
python

题目

 代码

import random
player_dict = {}       # 做最后输出字典
hand = []              # 做为判断重复牌的依据
# 发牌
def brand():
    num_size = "123456789JQKA"
    flower_color = ["♠","♣","♥","◆"]
    global hand_new                             # 声明一个全局变量,列表
    hand_new = []                               # 做为判断是否出循环的依据
    while True:
        a = random.choice(flower_color)         # 在列表中flower_color中,随机生成1个
        b = random.sample(num_size,1)           # 在字符串num_size中随机生成1个
        c = "".join(a+b[0])                     # 合并随机生成的内容
        if c not in hand:                       # 判断是否重复出现牌
            hand.append(c)
            hand_new.append(c)
        if len(hand_new) == 3:                  # 判断是否出循环
            break
#按名字数量调用发牌
def playing_surface():
    for i in names:                             # 按列表内的字符串数依次循环
        brand()                                 # 调用函数
        player_dict[i] = hand_new               # 加数据到字典
names = input("请输入玩家姓名(以逗号隔开):").split(",")
playing_surface()                               # 调用函数
print(player_dict)

有没有大佬指点一二萌新。。。

举报

相关推荐

0 条评论