本文主要是介绍Python代码实现2024年刘谦春晚魔术,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import randomdef 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 = 0while 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 = 1elif northOrSouth == "北方人":tag_north_or_south = 2else:tag_north_or_south = 3cardTackMergeTopAsTagNOrS = 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()
效果截图:
过程打印:
这篇关于Python代码实现2024年刘谦春晚魔术的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!