HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculate total profit/loss

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
totallosscalculateprofit

Problem

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:

add_selection =raw_input("Would you like to add a selection? ")
if add_selection == "Yes":
print
selection = raw_input('Horse: ')

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_

Solution

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: '))

Code Snippets

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

Context

StackExchange Code Review Q#138083, answer score: 3

Revisions (0)

No revisions yet.