# Calculate your odds of surviving the Murder Trivia Party chalices game
from collections import defaultdict

CHALICES = 8 # Total number of chalices
chances = {0: 1.0} # Initially, you have 100% chance of having no placed pellets.
total_chances = []
for poison in range(CHALICES): # though you won't normally see the full set
	# Add another poison pellet.
	newchance = defaultdict(float)
	for poisoned, prob in chances.items():
		# There are 'poisoned' chalices already poisoned when this pellet
		# gets added. Thus there are that many chances out of CHALICES to
		# re-poison a poisoned chalice.
		if poisoned:
			newchance[poisoned] += prob * poisoned / CHALICES
		# The remaining probability is that you poisoned a new one.
		true_brew = CHALICES - poisoned
		if true_brew:
			newchance[poisoned + 1] += prob * true_brew / CHALICES
		# This will always add up to the original probability.
		# The total probability in the dictionary will always equal 1.0.
	chances = newchance
	print("With %d poison pellets, your chances are:" % (poison + 1))
	tot = 0.0
	for poisoned, prob in sorted(chances.items()):
		survive = 100 - poisoned * 100.0 / CHALICES
		print("%5.2f%% chance of %d poisoned for %5.2f%% survival chance ==> %5.2f%%"
			% (prob*100, poisoned, survive, survive * prob))
		tot += survive * prob
	print("-- total probability of survival %5.2f" % tot)
	total_chances.append(tot)

print("Summary:")
print(" ", "  ".join(" %2s   " % (n+1) for n in range(CHALICES)))
print(" ", "  ".join("%5.2f%%" % c for c in total_chances))

# Summary:
#     1       2       3       4       5       6       7       8
#   87.50%  76.56%  66.99%  58.62%  51.29%  44.88%  39.27%  34.36%

@@ -106,48 +106,101 @@
 ict%0A
-%0ACHALICES = 8 # Total number of chalices
+import sys%0A%0Atry:%0A%09CHALICES = int(sys.argv%5B1%5D)%0Aexcept (IndexError, ValueError):%0A%09CHALICES = 8%0A
 %0Acha

Allow customization of the chalice count