python 一副牌随机生成一张牌
一副扑克牌包含4种花色,13个数值,1个小王,1个大王,实现随机生成一张牌 import random def access(): color = random.randint(0, 4) # randint() 随机一个整数,包含两端 0 1 2 3 4 flower = ['黑桃', '红心', '方块', '梅花', '王'] card = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] if color in range(0, 4): # 0 1 2 3 表示正常数 value = random.randint(0, 12) # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 elif color == 4: value = random.randint(0, 1) # 0 1 if value == 0: return "小王" if value == 1: return "大王" else: return "错误的牌" print(flower[color]) # 取flower[3] 是梅花 print(card[value]) return flower[color] + card[value] if __name__ == '__main__': print(access())
世界上最美的风景,是自己努力的模样