2016-03-03 70 views
0

我正在尝试在Python中创建一些东西,只是为了我自己,这将决定在我的情况下绘制某张卡片的可能性。甲板的大小由raw_input决定,就像您试图计算绘制概率的卡片数量一样。现在,我有:Python中的依赖概率

from __future__ import division 

t = True 
while True: 
    if t == True: 
     numCards = int(raw_input("How many cards in your deck? Type here: ")) 

     print "There are " + str(numCards) + " cards in your deck." 

     whatCards = raw_input("What card are you testing for how likely it is to draw them? Type here: ") 
     whatCards = whatCards.capitalize() 
     print whatCards + " is a great card!" 

     if whatCards.endswith("s"): 
      numCards2 = int(raw_input("How many " + whatCards + " are in it? ")) 
      whatCards = whatCards + "s" 
     elif whatCards.endswith("y"): 
      numCards2 = int(raw_input("How many " + whatCards[:-1] + "ies are in it? ")) 
     else: 
      numCards2 = int(raw_input("How many " + whatCards + "s are in it?")) 

     if numCards2 > 1: 
      print "Ok! There are " + str(numCards2) + " " + whatCards + "s" + "!" 
     else: 
      print "Alright. There is " + str(1) + " " + whatCards + "!" 



     '''probability = (numCards2/numCards) + (numCards2/(numCards-1)) + (numCards2/(numCards-2)) + \ 


(numCards2/(numCards-3)) + (numCards2/(numCards-4)) + (numCards2/(numCards-5))\ 
        + (numCards2/(numCards-6))''' 
    probability = numCards2/numCards 

    print "Results are approximate" 
    print "Decimal: " + str(probability) 

    if len(str(probability)) <= 3: 
     print "Percentage: " + str(probability)[2] + str(0) + "%" 
    else: 
     print "Percentage: " + str(probability)[2] + str(probability)[3] + "%" 

    print "Fraction: " + str(numCards2) + "/" + str(numCards) 
    t = False 

if t == False: 
    numCards = int(raw_input("How many cards in your deck? Type here: ")) 

    print "There are " + str(numCards) + " cards in your deck." 

    whatCards = raw_input("What card are you testing for how likely it is to draw them? Type here: ") 
    whatCards = whatCards.capitalize() 
    print whatCards + " is a great card!" 

    if whatCards.endswith("s"): 
     numCards2 = int(raw_input("How many " + whatCards + " are in it? ")) 
     whatCards = whatCards + "s" 
    elif whatCards.endswith("y"): 
     numCards2 = int(raw_input("How many " + whatCards[:-1] + "ies are in it? ")) 
    else: 
     numCards2 = int(raw_input("How many " + whatCards + "s are in it?")) 

    if numCards2 > 1: 
     print "Ok! There are " + str(numCards2) + " " + whatCards + "s" + "!" 
    else: 
     print "Alright. There is " + str(1) + " " + whatCards + "!" 



    '''probability = (numCards2/numCards) + (numCards2/(numCards-1)) + (numCards2/(numCards-2)) + \ 
        (numCards2/(numCards-3)) + (numCards2/(numCards-4)) + (numCards2/(numCards-5))\ 
        + (numCards2/(numCards-6))''' 
    probability = numCards2/numCards 

    print "Results are approximate" 
    print "Decimal: " + str(probability) 

    if len(str(probability)) <= 3: 
     print "Percentage: " + str(probability)[2] + str(0) + "%" 
    else: 
     print "Percentage: " + str(probability)[2] + str(probability)[3] + "%" 

    print "Fraction: " + str(numCards2) + "/" + str(numCards) 
    t = True 

正如你所看到的,就是它的解决目前仅是一个平局的可能性。不过,我的目标是让用户可以输入他们自己的牌数,所以他们有60张牌组,15张沼泽牌,7张牌。问题是,如果我想画出一张牌的牌(7),我不能仅仅计算出抽出60,然后59,然后58,下降到53的概率。那是因为如果他们画沼泽,概率下降,从60降到53不会。我需要做些什么才能从y(纸牌大小)中随机选择x张牌,其中z的抽签量以及当“抽取”时x的数量递减,并且为每个“抽签”更改y。而且它将不得不工作许多次,这取决于用户想要的东西。谢谢! (希望这是有道理的......)

回答

0

假设你有n张卡片,并且你正在绘制m张卡片。可能的组合是n!/(n-m)!如果您正在寻找抽出总数为n张牌的x牌的概率(y < = m)。您必须考虑2个数量: 1.从x卡中挑选y卡的可能方式是x!/(x-y)!然后可能的方法是将这些有序集合放入m个插槽中,或者将其他m-y卡片放入m!/(m-y)的一组m卡中! 这两个数量的产品给你有利的事件,而第一个数字给你的总事件。所以,你的概率是

P = X×!M!(NM)!/(N!(XY)!(我的)!)

我希望我没有错过任何东西:)

+0

感谢帮帮我!只是好奇,'!'是什么意思?做? – Oughh

+0

是否应该有东西而不是!'s?顺便说一下,我使用的是Python,它在!上抛出了一个语法错误。 – Oughh

+0

其因子5! = python中的5x4x3x2x1是'math.factorial(x)' –