20210920NebulaChien的博客CSDN博客 两个人用猜拳的方式决定胜负,所有出拳的组合有3x39种,其中A胜、B胜和平局的组合各有三种,也就是说每一种结局对应的概率为13,那么这个过程到底是如何是实现的呢?下面我们用Python模拟这一过程。 代码如下,用到列表索引、random库PossiblityofmoragamefromrandomimportmoraGamerA〔Rock,Paper,Scissor〕moraGamerB〔Rock,Paper,Scissor〕countAwin0。0countBwin0。0countDraw0。0Flag1whileFlag:roundint(input(Enetrrounds:(rounds10)))usewhileofiftomakesuretheiriscorrectindexforrangeifround10:print(Theinputbureaudoesnotmeettherequirements)Flag0breakelse:forroundinrange(1,round1):print(Gameround{}。format(round))guessAmoraGamerA〔randint(0,2)〕guessBmoraGamerB〔randint(0,2)〕Attentiontherangeoflistindexif(guessARockandguessBScissor)or(guessAPaperandguessBRock)or(guessAScissorandguessBPaper):countAwin1print(Ais{},Bis{}。format(guessA,guessB))print(GamerAiswinner)elifguessAguessB:countDraw1print(AandBis{}。format(guessA))print(Draw)else:countBwin1print(Ais{},Bis{}。format(guessA,guessB))print(GamerBiswinner)print(In{}rounds,gamerAhavewon{}roundsandgamerBhavewon{}rounds,{}roundsisdraw。。format(round,countAwin,countBwin,countDraw))print(ThepossiblityofAwinis{}。format(countAwinround))print(ThepossiblityofBwinis{}。format(countBwinround))print(Thepossiblityofdrawis{}。format(countDrawround))Flagint(input(Enter1tocontinueand0toexit:))ifFlag!1andFlag!0:Flag0print(Forceexitbecausemistakeinput) 可以看到,并不是每次输入一个rounds都对应13的结果,因为13是理想条件下根据大量实验统计得到的,而随着rounds逐渐增大,乃至于大于1000000,得出的结果会越来越逼近0。333333。。。 emmmm,其实写这段代码是放假无聊找点事做,因为计二报的是Python,虽然有比较强的基础,但还是看看书,顺带熟悉一下各种库的用法,random库是真的蛮有意思。 第一次在文章里面嵌入代码不太熟练,缩进什么的都惨不忍睹〔捂脸〕 各位小伙伴将就一下〔谢谢〕 目前大二,物联网专业,以后有空会分享一些有趣的学习成果,大家有什么想了解的也可以打在评论区,算法、硬件、专业等等,如有不严谨之处,求求某位大佬出来教我做事〔流泪〕 祝各位同学,各位大佬中秋节快乐!〔比心〕