我正在嘗試在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。而且它將不得不工作許多次,這取決於用戶想要的東西。謝謝! (希望這是有道理的......)
感謝幫幫我!只是好奇,'!'是什麼意思?做? – Oughh
是否應該有東西而不是!'s?順便說一下,我使用的是Python,它在!上拋出了一個語法錯誤。 – Oughh
其因子5! = python中的5x4x3x2x1是'math.factorial(x)' –