Optimized some older code.

This commit is contained in:
lukeroge 2012-04-23 13:16:08 +12:00
parent 99ce2f09a6
commit b5fbcaaefc

View file

@ -1,14 +1,13 @@
# Created by Lukeroge, improved by TheNoodle # Created by Lukeroge, improved by TheNoodle
from util import hook from util import hook
import random from random import getrandbits
# used for tails: x heads: y # used for tails: x heads: y
def flip_simple(count): def flip_simple(count):
heads = 0 heads, tails = 0, 0
tails = 0 for x in xrange(count):
for x in range(count): c = getrandbits(1)
c = random.randint(0, 1)
if c == 0: if c == 0:
heads += 1 heads += 1
else: else:
@ -33,13 +32,12 @@ def coin(inp):
return "Too many coins! Maximum is 9001." return "Too many coins! Maximum is 9001."
# depending on the count, we use two different methods to get the output # depending on the count, we use two different methods to get the output
if count == 1: if count == 1:
flip = random.randint(0, 1) flip = getrandbits(1)
if flip == 1: if flip == 1:
sidename = "heads" return "You flip a coin and get heads."
else: else:
sidename = "tails" return "You flip a coin and get tails."
return "You flip a coin and get " + sidename + "."
else: else:
flips = flip_simple(count) flips = flip_simple(count)
return "You flip %s coins and get " \ return "You flip %i coins and get " \
"%s heads and %s tails." % (str(count), str(flips[0]), str(flips[1])) "%i heads and %i tails." % (count, flips[0], flips[1])