2016-03-03 55 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)' –