import random
def main():
# 扑克牌随机抽取4张牌
playingCards = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
cardTackA = []
for i in range(4):
k = random.choice(playingCards)
cardTackA.append(k)
# 将抽取的4张牌随机打乱
cnt = 0
while cnt < 100:
random.shuffle(cardTackA)
cnt += 1
# 将牌对折从中间撕开
cardTackB = cardTackA
# 再面向同一个方向叠起来
cardTackMerge = cardTackA + cardTackB
# 根据名字的数量将最上面的相应数量的牌挪到最下面
for i in range(len(myName)):
cardTackMerge.append(cardTackMerge[0])
cardTackMerge.remove(cardTackMerge[0])
# 将最上面三张插入到剩下的里面的中间随机位置
cardTackMergeTopThree = cardTackMerge[0:3]
for i in range(3):
cardTackMerge.remove(cardTackMerge[0])
toIndex = random.randint(0, len(cardTackMerge)-2)
cardTackMerge[toIndex + 1: toIndex + 1] = cardTackMergeTopThree
# 取出第一张藏在一边
hidden = cardTackMerge[0]
cardTackMerge.remove(cardTackMerge[0])
# 根据是南方人还是北方人取前几张并插到剩下的中间随机位置
if northOrSouth == "南方人":
tag_north_or_south = 1
elif northOrSouth == "北方人":
tag_north_or_south = 2
else:
tag_north_or_south = 3
cardTackMergeTopAsTagNOrS = cardTackMerge[0:tag_north_or_south]
for i in range(tag_north_or_south):
cardTackMerge.remove(cardTackMerge[0])
toIndex1 = random.randint(0, len(cardTackMerge)-2)
cardTackMerge[toIndex1 + 1: toIndex1 + 1] = cardTackMergeTopAsTagNOrS
# 男生扔掉1张,女生扔掉2张
if sex == "男":
cardTackMerge.remove(cardTackMerge[0])
elif sex == "女":
cardTackMerge.remove(cardTackMerge[0])
cardTackMerge.remove(cardTackMerge[0])
else:
print("Error: 性别(sex)参数输入错误!!!")
return "Error"
# 根据"见证奇迹的时候"这几个字,将牌依次挪到最下面(念一个字挪一次)
for i in range(len("见证奇迹的时候")):
tmpTopOne = cardTackMerge[0]
cardTackMerge.remove(cardTackMerge[0])
cardTackMerge.append(tmpTopOne)
# "好运留下来,烦恼丢出去",念到前半句将最上面的牌挪到最下面,念到后半句则将最上面的牌丢出去,依次循环,男生5次,女生4次
if sex == "男":
for i in range(5):
topOne = cardTackMerge[0]
cardTackMerge.remove(cardTackMerge[0])
cardTackMerge.append(topOne)
cardTackMerge.remove(cardTackMerge[0])
elif sex == "女":
for i in range(4):
topOne = cardTackMerge[0]
cardTackMerge.remove(cardTackMerge[0])
cardTackMerge.append(topOne)
cardTackMerge.remove(cardTackMerge[0])
print("随机抽取四张牌: {}\n之前藏的半张牌: {}\n最后留的半张牌: {}\n这两个是否一样: {}".format(", ".join(cardTackA), hidden, cardTackMerge[0], "是" if hidden == cardTackMerge[0] else "否"))
if __name__ == '__main__':
myName = "木子"
northOrSouth = "南方人"
# northOrSouth = "北方人"
sex = "男"
# sex = "女"
main()
效果截图:
过程打印: