# Calculate total profit/loss

This program starts with initial balance and the individual book balances which must match the initial balance to proceed. The user then enters the selections for the day along with stake, odds etc., then the program calculates the new balance with the new book balances.

def main():
def balances(msg):
while True:
try:
x = float(raw_input(msg))
return x
except ValueError:
print"That's not a number"
continue

while True:
balance = balances('Balance:')
print
bfair_balance = balances('bfair:')
wh_balance = balances('wh:')
freds_balance = balances('freds:')
sky_balance = balances('sky:')
pp_balance = balances('pp:')
balance_sum = pp_balance + bfair_balance + sky_balance + freds_balance + wh_balance
if balance == balance_sum:
# balance is correct -> stop the loop
break
else:
print "Balances do not match"
print

print "Balance: %s" %balance
print "Bfair: %d, Sky: %d, pp: %d, freds: %d, wh: %d" %(bfair_balance, sky_balance, pp_balance, freds_balance, wh_balance)

books = [bfair_balance, wh_balance, sky_balance,freds_balance,pp_balance]
#print books

print

inputs = []
new_books = []

def looks_good(inputs):
for i in inputs:
print i

while True:

print
selection = raw_input('Horse: ')

stake = float(raw_input('Stake: '))
while stake <=0:
print "Please enter a stake greater than 0"
stake = float(raw_input('Stake: '))
while stake > bfair_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > pp_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > freds_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > sky_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > wh_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))

odds = float(raw_input('Odds: '))
while odds <=0:
print "Please enter odds greater than 0"
odds = float(raw_input('Odds: '))

result = (raw_input('Result: '))
if result == "Win":
result = stake * odds
print "Returns:%d"%result
elif result == "Lose":
result = 0 * odds
print "Returns:%d"%result

book = raw_input('Book: ')

while book not in['bfair','sky','wh','freds','pp']:
print "That's not valid"
book = raw_input('Book: ')

if result == 0 and book == "bfair":
bfair_balance = bfair_balance - stake
new_books.append(bfair_balance)
elif result == (stake * odds) and  book == "bfair":
bfair_balance = bfair_balance - stake + (stake * odds)
new_books.append(bfair_balance)

if result == 0 and book == "freds":
freds_balance = freds_balance - stake
new_books.append(freds_balance)
elif result == (stake * odds) and  book == "freds":
freds_balance = freds_balance - stake + (stake * odds)
new_books.append(freds_balance)

if result == 0 and book == "pp":
pp_balance = pp_balance - stake
new_books.append(pp_balance)
elif result == (stake * odds) and  book == "pp":
pp_balance = pp_balance - stake + (stake * odds)
new_books.append(pp_balance)

if result == 0 and book == "wh":
wh_balance = wh_balance - stake
print wh_balance - stake
new_books.append(wh_balance)
elif result == (stake * odds) and  book == "wh":
wh_balance = wh_balance - stake + (stake * odds)
new_books.append(wh_balance)

if result == 0 and book == "sky":
sky_balance = sky_balance - stake
new_books.append(sky_balance)
elif result == (stake * odds) and  book == "sky":
sky_balance = sky_balance - stake + (stake * odds)
new_books.append(sky_balance)

my_list=[selection,stake,odds,result,book]

inputs.append(my_list)
print
total_stake=[]
for my_list in inputs:
total_stake.append(my_list)
print "Total Stake: %d" %sum(total_stake)

total_winnings = []
for my_list in inputs:
total_winnings.append(my_list)
print "Total Winnings: %d" %sum(total_winnings)
print
new_balance = balance - sum(total_stake) + sum(total_winnings)
print "New Balance:%d" %new_balance

print "Bfair: %d, Sky: %d, pp: %d, freds: %d, wh: %d" %(bfair_balance, sky_balance, pp_balance, freds_balance, wh_balance)
print

break

looks_good(inputs)

import os
os.system("pause")

if __name__ == '__main__':
main()


### Use loops to avoid repetition

For example you have:

while stake > bfair_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
# many very similar blocks


You should use a loop to avoid repetition:

for balance in [bfair_balance, ...]:
while stake > balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))

• Well thank you. It looks like I need to try harder and perfect the code before asking on here. It turns out I need to use a dictionary rather than a list. I want to see how far I can get wihtout posting on here, thanks for the help mate. Aug 9 '16 at 1:53