Python 以“计算平均值”;石头、布、剪刀”;

Python 以“计算平均值”;石头、布、剪刀”;,python,average,Python,Average,昨晚我在想,在《石头、布、剪刀》中,我有可能连续10次得到同样的结果。我想出了如何做到这一点并完成了那项任务,但后来我想挑战一下自己,所以我想调整程序,让它运行初始程序数次(10000次),然后输出平均结果,我希望这将接近连续10次获得相同结果的概率。请注意,我没有考虑战术,只是两名球员随机选择r、p或s def rps(): num=0 # num is the number of times i want the programme to run while roll<10:

昨晚我在想,在《石头、布、剪刀》中,我有可能连续10次得到同样的结果。我想出了如何做到这一点并完成了那项任务,但后来我想挑战一下自己,所以我想调整程序,让它运行初始程序数次(10000次),然后输出平均结果,我希望这将接近连续10次获得相同结果的概率。请注意,我没有考虑战术,只是两名球员随机选择r、p或s

def rps():

   num=0 # num is the number of times i want the programme to run while roll<10: 
   total=0 # this should be close to 3^10 * 10000, its the result of all the tries added together 

   while num <10001:
      tries=0 # this is how many times the programme has tried to get p1=p2
      roll=0 # this is the amount of times it has counted p1=p2 (it gets re-set everytime it reaches 10)
      import random
      while roll <10:
         p1=random.randint(1,3)
         p2=random.randint(1,3)
         if p1==p2:
            roll=roll+1
         else:
            tries=tries + 1
            roll=0    
            num=num+1
         if roll==10:
            total=total+roll
            roll=0
      if num==10000:
         print (num)
         print (total/num)
rps()
def rps():

num=0#num是我希望程序在滚动一些自解释代码时运行的次数(因此您还可以学习编写干净的代码):


这个程序有很多问题,这一次,第二个for循环没有任何用处,我知道如果连续转鼓数达到10,你正在使用第二个for循环重置计数器,但你已经在这里这么做了

if roll==10:
    total=total+roll
    roll=0
通过设置roll=0,可以重置它。 另外,最后一个if条件增加了复杂性

if num==10000:
         print (num)
         print (total/num)
与此相反,您可以像这样打印循环外的平均值

if roll==10:
    total=total+roll
    roll=0
print (total/10000) #this being outside the while loop
还有一件事,只有在roll1!=roll2,这增加了循环必须运行的次数 这就是改变后的节目

import random
def rps():
    num=0 #num is the number of times i want the programme to run while roll<10:
    total=0 #this should be close to 3^10 * 10000, its the result of all the tries added together
    roll = 0
    while num <10001:
        tries=0 #this is how many times the programme has tried to get p1=p2
        p1=random.randint(1,3)
        p2=random.randint(1,3)
        if p1==p2:
            roll = roll+1
        else:
            tries = tries + 1
            roll = 0
        if roll==10:
            print(roll)
            total += roll
            roll=0
        num = num + 1
    print (total/10000)
    print(tries)
rps()
随机导入
def rps():

num=0#num是我希望程序在滚动时运行的次数。为什么是向下投票?乔已经解释了他想要计算的内容,并发布了一些相关代码。当然,代码的逻辑并不完全符合他的要求,但这并不是否决这个问题的理由。我不明白为什么@Joe会得到这么多否决票!顺便说一句,
import
语句应该在脚本的开头。它们绝对不应该放在循环内。它不会使程序发生故障,因为Python不会重新导入它已经加载的模块,但它仍然必须在每次点击循环内的
import
时检查它是否加载了,这是低效的。如果不清楚,通过“相同的结果”,Joe希望找到10个“平局”的几率谢谢,但我想知道如何更改我的代码不是一个全新的代码我不确定这是否正确,因为某些原因,当你打印的时候,它的总数为0,而它应该等于10000*3^10左右。我不认为我完全理解你想说的话,你的意思是说total是程序玩游戏的总次数吗?因为在程序中,total是两个玩家连续10个任期获得相同输出的总次数。
import random
def rps():
    num=0 #num is the number of times i want the programme to run while roll<10:
    total=0 #this should be close to 3^10 * 10000, its the result of all the tries added together
    roll = 0
    while num <10001:
        tries=0 #this is how many times the programme has tried to get p1=p2
        p1=random.randint(1,3)
        p2=random.randint(1,3)
        if p1==p2:
            roll = roll+1
        else:
            tries = tries + 1
            roll = 0
        if roll==10:
            print(roll)
            total += roll
            roll=0
        num = num + 1
    print (total/10000)
    print(tries)
rps()