2012-02-20 04:34:17 +01:00
|
|
|
# Created by Lukeroge, improved by TheNoodle
|
2011-11-20 10:23:31 +01:00
|
|
|
from util import hook
|
2012-04-23 03:16:08 +02:00
|
|
|
from random import getrandbits
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-02-29 06:47:11 +01:00
|
|
|
|
2012-02-20 04:34:17 +01:00
|
|
|
# used for tails: x heads: y
|
2011-12-01 17:13:08 +01:00
|
|
|
def flip_simple(count):
|
2012-04-23 03:16:08 +02:00
|
|
|
heads, tails = 0, 0
|
|
|
|
for x in xrange(count):
|
|
|
|
c = getrandbits(1)
|
2011-12-02 14:42:36 +01:00
|
|
|
if c == 0:
|
|
|
|
heads += 1
|
2011-12-01 17:13:08 +01:00
|
|
|
else:
|
2011-12-02 14:42:36 +01:00
|
|
|
tails += 1
|
2012-02-29 06:47:11 +01:00
|
|
|
return [heads, tails]
|
2011-12-02 14:42:36 +01:00
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2011-12-01 17:13:08 +01:00
|
|
|
@hook.command(autohelp=False)
|
|
|
|
def coin(inp):
|
2012-02-28 03:03:43 +01:00
|
|
|
".coin [amount] -- Flips [amount] of coins."
|
2012-02-29 06:47:11 +01:00
|
|
|
# checking for valid input. if valid input [count=inp],
|
|
|
|
# if invalid [return error], if no input [count=1]
|
2011-12-01 17:13:08 +01:00
|
|
|
if inp.isdigit():
|
|
|
|
count = int(inp)
|
|
|
|
else:
|
|
|
|
if inp:
|
2012-02-17 01:46:09 +01:00
|
|
|
return "Invalid input."
|
2011-12-01 17:13:08 +01:00
|
|
|
else:
|
|
|
|
count = 1
|
2012-02-17 01:46:09 +01:00
|
|
|
|
2012-04-23 02:54:00 +02:00
|
|
|
if count > 9001:
|
|
|
|
return "Too many coins! Maximum is 9001."
|
2011-12-02 14:42:36 +01:00
|
|
|
# depending on the count, we use two different methods to get the output
|
|
|
|
if count == 1:
|
2012-04-23 03:16:08 +02:00
|
|
|
flip = getrandbits(1)
|
2011-12-01 17:13:08 +01:00
|
|
|
if flip == 1:
|
2012-04-23 03:16:08 +02:00
|
|
|
return "You flip a coin and get heads."
|
2011-12-01 17:13:08 +01:00
|
|
|
else:
|
2012-04-23 03:16:08 +02:00
|
|
|
return "You flip a coin and get tails."
|
2011-12-01 17:13:08 +01:00
|
|
|
else:
|
2011-12-02 14:42:36 +01:00
|
|
|
flips = flip_simple(count)
|
2012-04-23 03:16:08 +02:00
|
|
|
return "You flip %i coins and get " \
|
|
|
|
"%i heads and %i tails." % (count, flips[0], flips[1])
|