Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算获胜的百分比_Python - Fatal编程技术网

Python 计算获胜的百分比

Python 计算获胜的百分比,python,Python,我正在制作一个程序,根据获胜的百分比显示一个随机的赢家。这里有一个算法可以帮助您更好地理解: 询问多个用户的姓名 程序应该询问他们是否需要更多的玩家 如果他们这样做了,那么再增加3名球员。如果没有,请继续 完成后,计算0-100之间的随机整数 向第一个玩家显示这个数字,以及他们必须赢的百分比,然后是第二个,然后是第三个,等等(他们的数字越接近100,百分比越高) 选择一个随机的赢家,百分比越高的人获胜的可能性越大 感谢用户玩游戏,并询问他们是否想再次玩或退出 如何根据随机数与100的接近程度确定

我正在制作一个程序,根据获胜的百分比显示一个随机的赢家。这里有一个算法可以帮助您更好地理解:

  • 询问多个用户的姓名
  • 程序应该询问他们是否需要更多的玩家
  • 如果他们这样做了,那么再增加3名球员。如果没有,请继续
  • 完成后,计算0-100之间的随机整数
  • 向第一个玩家显示这个数字,以及他们必须赢的百分比,然后是第二个,然后是第三个,等等(他们的数字越接近100,百分比越高)
  • 选择一个随机的赢家,百分比越高的人获胜的可能性越大
  • 感谢用户玩游戏,并询问他们是否想再次玩或退出
  • 如何根据随机数与100的接近程度确定适当的百分比?

    我理解其中一些代码可能是不相关的,但我希望通过理解整个程序使我的问题尽可能有意义-这对您有更多帮助

    以下是我目前的代码:

    #Luck Draw#
    #=========#
    import time
    import random
    
    print("You will be given a number, the closer your number to 100, the higher the chance you win!")
    
    print("Player 1, please input your name.")
    player1 = input()
    
    print("Player 2, please input your name.")
    player2 = input()
    print("Player 3, please input your name.")
    player3 = input()
    
    morePlayers = input("Would you like more players?")
    morePlayers = morePlayers.upper()
    
    if morePlayers == 'YES':
        print("Player 4, please input your name.")
        player4 = input()
        print("Player 5, please input your name.")
        player5 = input()
        print("Player 6, please input your name.")
        player6 = input()
        print("** Unfortunately this program does not support more than six players yet! **")
        time.sleep(2.5)
    
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player1num = random.randint(0,100)
        print(player1 + " Your number is:", str(player1num) + "! You have a percantage to win of: ")
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player2num = random.randint(0,100)
        print(player2 + " Your number is:", str(player2num) + "! You have a percantage to win of: ")
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player3num = random.randint(0,100)
        print(player3 + " Your number is:", str(player3num) + "! You have a percantage to win of: ")
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player4num = random.randint(0,100)
        print(player4 + " Your number is:", str(player4num) + "! You have a percantage to win of: ")
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player5num = random.randint(0,100)
        print(player5 + " Your number is:", str(player5num) + "! You have a percantage to win of: ")
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player6num = random.randint(0,100)
        print(player6 + " Your number is:", str(player6num) + "! You have a percantage to win of: ")
    
        print("And the winner is...")
        time.sleep(1)
        print("...")
        time.sleep(1)
        print(winner + "!")
    else:
        print("Calculating...")
        player1num = random.randint(0,100)
        print(player1 + " Your number is:", str(player1num) + "! You have a percantage to win of: ")
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player2num = random.randint(0,100)
        print(player2 + " Your number is:", str(player2num) + "! You have a percantage to win of: ")
        time.sleep(0.75)
        print("Calculating...")
        time.sleep(3)
        player3num = random.randint(0,100)
        print(player3 + " Your number is:", str(player3num) + "! You have a percantage to win of: ")
    

    我想你要找的是随机抽样,每个项目都有概率。你会想利用numpy图书馆

    像这样的东西可能对你有用

    import numpy as np
    players = ["fred", "alice", "mary"]
    probabilities = [0.2, 0.7, 0.1]     # probabilities of each player
    winner = np.random.choice(players, p=probabilities)
    
    希望有帮助


    罗尼

    一名球员获得的随机数是否会作为他们获胜的几率?例如,玩家1得到数字80,因此他们应该有80%的获胜几率,玩家2得到数字0,并且不会获胜,玩家3得到数字20,并且有20%的获胜几率?好吧,你不需要任何百分比来开始。这对你来说确实是个问题:你希望你的规则是什么?如果人数最多的玩家获胜,那么你已经拥有了一切。如果任何获得超过,比如说N的人获胜,你需要引入该常数并将其值与之进行比较。如果你想按比例分配奖金,那么,按比例分配什么?试着澄清你想要达到的目标。首先,伊万。我想我的程序来计算一个近似的百分比取决于数字。我自己也不知道规则,所以我才这么问。第二,阿尔夫。谢谢你的深入解释,但是,你有什么想法或规则,我可以用来计算这个百分比吗?如果你有75/100,你的问题就没有意义了。。。这实际上意味着75%的随机选取的数字将小于您的数字,而只有25%将高于您的数字…让我详细说明一下,我知道混淆可能来自何处。可以所以,假设用户Jon得到数字58,我希望根据其他用户的数字来划分他们的百分比。Jon=58 Marie=37 Bob=19 Jon获胜的几率更高,但我如何才能得到Jon的百分比呢?我自己真的很困惑,我只是觉得增加一个百分比是一个很好的方式来告诉球员他们的机会。