coin.py is now a nest og if statements. Huzzah

This commit is contained in:
Luke Rogers 2011-12-02 05:13:08 +13:00
parent c4575a8e89
commit cd9e35ed33

View file

@ -2,38 +2,62 @@
from util import hook from util import hook
import random import random
## NOTES:
## Need to make a function to return a number of flips
# this produces a string full of comma seperated coin flips - should output this as a list
def flip_list(count):
out = ""
for i in range(count):
flip = random.randint(0,1)
if flip == 1:
if i == 0:
out += "Heads"
else:
out += ", Heads"
else:
if i == 0:
out += "Tails"
else:
out += ", Tails"
return out
# this doesn't work (yet)
def flip_simple(count):
out = ""
heads = 0
tails = 0
for i in range(count):
flip = random.randint(0,1)
if flip == 1:
heads = heads + 1
else:
tails = tails + 1
return msg
@hook.command(autohelp=False) @hook.command(autohelp=False)
def coin(inp): def coin(inp):
".coin [amount] -- flips some coins and shares the result." ".coin [amount] -- flips some coins and shares the result."
# checking for valid input. if valid input [count=inp], if invalid [return error], if no input [count=1]
if inp.isdigit(): if inp.isdigit():
count = int(inp) count = int(inp)
else:
if inp:
return "Invalid Input :("
else: else:
count = 1 count = 1
if count > 10: # depending on the count, we use three different methods to get the output
return "Maximum amount of coins is ten." if count <= 10 and count > 1:
msg = "You flip " + str(count) + " coins and get the following results: " + flip_list(count)
if count > 1: return msg
coin = "coins" elif count == 1:
else:
coin = "coin"
msg = "You flip " + str(count) + " " + coin + " and get the following results:"
for i in range(count):
flip = random.randint(0,1) flip = random.randint(0,1)
if flip == 1: if flip == 1:
if i == 0: return "You flip a coin and get heads."
msg += " Heads"
else: else:
msg += ", Heads" return "You flip a coin and get tails."
else: else:
if i == 0: return "Amounts over ten are not yet implemented"
msg += " Tails"
else:
msg += ", Tails"
return msg