After testing pokers hands x amount of times, I need to print a table with the title of the hand (full house, flush, etc.), the number of times each of them occurred, the percentage of times they occurred, expected percentage, and difference between the two. It won't let me run it with more than 2 iterations.
Here is what I have:
def is_pair(hand):
Yes = 0
for card in hand:
count = 0
for i in hand:
if (card['value']) == (i['value']):
count += 1
if count == 2:
Yes = 1
if Yes == 0:
return False
else:
if is_full_house(hand) is True:
return False
elif is_2_pair(hand) is True:
return False
elif is_3_of_a_kind(hand) is True:
return False
elif is_4_of_a_kind(hand) is True:
return False
else:
return True
def is_2_pair(hand):
Yes = 0
for card in hand:
count = 0
for i in hand:
if card['value'] == i['value']:
count += 1
if count == 2:
Yes += 1
if Yes == 4:
if is_4_of_a_kind(hand) is True:
return False
elif is_full_house(hand) is True:
return False
else:
return True
else:
return False
def is_3_of_a_kind(hand):
Yes = 0
for card in hand:
count = 0
for i in hand:
if card['value'] == i['value']:
count += 1
if count == 3:
Yes = 1
if Yes == 0:
return False
else:
if is_full_house(hand) is True:
return False
else:
return True
def is_4_of_a_kind(hand):
Yes = 0
for card in hand:
count = 0
for i in hand:
if card['value'] == i['value']:
count += 1
if count == 4:
Yes = 1
if Yes == 0:
return False
else:
return True
def is_full_house(hand):
Yes = 0
if is_3_of_a_kind(hand) is True:
Yes += 1
else:
return False
if is_pair(hand) is True:
Yes += 1
else:
return False
if Yes == 2:
return True
else:
return False
def is_flush(hand):
Yes = 0
for card in hand:
count = 0
for i in hand:
if card['suit'] == i['suit']:
count += 1
if count == 5:
Yes = 1
if Yes == 0:
return False
else:
return True
def is_straight(hand):
list = []
for card in hand:
list.append(card['value'])
list.sort()
if is_pair(hand) is True:
return False
elif is_2_pair(hand) is True:
return False
elif is_3_of_a_kind(hand) is True:
return False
elif is_4_of_a_kind(hand) is True:
return False
elif is_full_house(hand) is True:
return False
elif list[4] - list [0] == 4:
return True
else:
return False
def is_high_card(hand):
if (is_pair(hand) is False) and (is_2_pair(hand) is False) and (is_3_of_a_kind(hand) is False) and (is_4_of_a_kind(hand) is False) and (is_flush(hand) is False) and (is_full_house(hand) is False) and (is_straight(hand) is False) and (is_straight_flush(hand) is False):
return True
else:
return False
def is_straight_flush(hand):
if (is_straight(hand) is True) and (is_flush(hand) is True):
return True
else:
return False
def main():
deck = build_deck()
shuffle(deck)
hand = deck[:5]
return hand
def tests():
hand = main()
if is_straight_flush(hand) is True:
return('Straight flush')
elif is_4_of_a_kind(hand) is True:
return('Four of a kind')
elif is_full_house(hand) is True:
return('Full house')
elif is_flush(hand) is True:
return('Flush')
elif is_straight(hand) is True:
return('Straight')
elif is_3_of_a_kind(hand) is True:
return('Three of a kind')
elif is_2_pair(hand) is True:
return('Two pairs')
elif is_pair(hand) is True:
return('One pair')
elif is_high_card(hand) is True:
return('High card')
def main2():
iterations = int(input("How many hands would you like to test? "))
hands = ['Straight flush', 'Four of a kind', 'Full house', 'Flush', 'Straight', 'Three of a kind', 'Two pair', 'One pair', 'High card']
sf_expected = round(40/2598960*100, 4)
fok_expected = round(624/2598960*100, 4)
fh_expected = round(3744/2598960*100, 4)
f_expected = round(5108/2598960*100, 4)
s_expected = round(10200/2598960*100, 4)
tok_expected = round(54912/2598960*100, 4)
tp_expected = round(123552/2598960*100, 4)
op_expected = round(1098240/2598960*100, 4)
hc_expected = round(1302540/2598960*100, 4)
sf_freq = 0
fok_freq = 0
fh_freq = 0
f_freq = 0
s_freq = 0
tok_freq = 0
tp_freq = 0
op_freq = 0
hc_freq = 0
for i in range(iterations):
tests()
if (tests() == 'Straight flush'):
sf_freq += 1
if (tests() == 'Four of a kind'):
fok_freq += 1
if (tests() == 'Full house'):
fh_freq += 1
if (tests() == 'Flush'):
f_freq += 1
if (tests() == 'Straight'):
s_freq += 1
if (tests() == 'Three of a kind'):
tok_freq += 1
if (tests() == 'Two pair'):
tp_freq += 1
if (tests() == 'One pair'):
op_freq += 1
if (tests() == 'High card'):
hc_freq += 1
occurences = [sf_freq, fok_freq, fh_freq, f_freq, s_freq, tok_freq, tp_freq, op_freq, hc_freq]
expected = [sf_expected, fok_expected, fh_expected, f_expected, s_expected, tok_expected, tp_expected, op_expected, hc_expected]
percent = [sf_freq/iterations * 100, fok_freq/iterations * 100, fh_freq/iterations * 100, f_freq/iterations * 100, s_freq/iterations * 100, tok_freq/iterations * 100, tp_freq/iterations * 100, op_freq/iterations * 100, hc_freq/iterations * 100]
difference = [sf_freq/iterations * 100 - sf_expected, fok_freq/iterations * 100 - fok_expected, fh_freq/iterations * 100 - fh_expected, f_freq/iterations * 100 - f_expected, s_freq/iterations * 100 - s_expected, tok_freq/iterations * 100 - tok_expected, tp_freq/iterations * 100 - tp_expected, op_freq/iterations * 100 - op_expected, hc_freq/iterations * 100 - hc_expected]
print("{:<15} {:<15} {:<15} {:<15} {:>15}".format('Hand', 'Occurences','Percent', 'Expected', 'Difference'))
all = [['Straight flush', sf_freq, sf_freq/iterations * 100, sf_expected, sf_freq/iterations * 100 - sf_expected],
['Four of a kind', fok_freq, fok_freq/iterations * 100, fok_expected, fok_freq/iterations * 100 - fok_expected],
['Full house', fh_freq, fh_freq/iterations * 100, fh_expected, fh_freq/iterations * 100 - fh_expected],
['Flush', f_freq, f_freq/iterations * 100, f_expected, f_freq/iterations * 100 - f_expected],
['Straight', s_freq, s_freq/iterations * 100, s_expected, s_freq/iterations * 100 - s_expected],
['Three of a kind', tok_freq, tok_freq/iterations * 100, tok_expected, tok_freq/iterations * 100 - tok_expected],
['Two pair', tp_freq, tp_freq/iterations * 100, tp_expected, tp_freq/iterations * 100 - tp_expected],
['One pair', op_freq, op_freq/iterations * 100, op_expected, op_freq/iterations * 100 - op_expected],
['High card', hc_freq, hc_freq/iterations * 100, hc_expected, hc_freq/iterations * 100 - hc_expected]]
for list in all:
hands, occurences, percent, expected, difference = list
print("{:<15} {:<15} {:<15} {:<15} {:>15}".format(hands, occurences, percent, expected, difference))
main2()