Skip to main content
Post Reopened by Jamal
deleted 71 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

This program starts with initial balance and the individual book balances which must match the initial balance to proceed.

User 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.

I hope someone can help me make this code better thank you.

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 <=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[1])
            print "Total Stake: %d" %sum(total_stake)

            total_winnings = []
            for my_list in inputs:
                total_winnings.append(my_list[3])
            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

        elif add_selection == "No":
             break



        looks_good(inputs)



    import os
    os.system("pause")

if __name__ == '__main__':
            main()
   

This program starts with initial balance and the individual book balances which must match the initial balance to proceed.

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.

I hope someone can help me make this code better thank you.

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 <=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[1])
            print "Total Stake: %d" %sum(total_stake)

            total_winnings = []
            for my_list in inputs:
                total_winnings.append(my_list[3])
            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

        elif add_selection == "No":
             break



        looks_good(inputs)



    import os
    os.system("pause")

if __name__ == '__main__':
            main()
   

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 <=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[1])
            print "Total Stake: %d" %sum(total_stake)

            total_winnings = []
            for my_list in inputs:
                total_winnings.append(my_list[3])
            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

        elif add_selection == "No":
             break



        looks_good(inputs)



    import os
    os.system("pause")

if __name__ == '__main__':
            main()
formatting
Source Link

I am sure Ihope someone can avoid the repetition in the code someway but my def(bb) doesnt work.

I am grateful for any help I can get withme make this code better thank you.

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)

    #booksbooks = [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 <=0:
                print "Stake"Please cannotenter bea negative"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 "Odds"Please cannotenter beodds negative"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: ')


# try get this into a function?

            def bb(book):
                while book in ['bfair','sky','wh','freds','pp']:
                    if result == 0:
                        book_balance = book_balance - stake
                        new_books.append(book_balance)
                    elif result == (stake * odds):
                        book_balance = book_balance - stake + (stake * odds)
                        new_books.append(book_balance)
                bb(bfair)
                bb(freds)
                bb(sky)
                bb(pp)
                bb(wh)


            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[1])
            print "Total Stake: %d" %sum(total_stake)

            total_winnings = []
            for my_list in inputs:
                total_winnings.append(my_list[3])
            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

        elif add_selection == "No":
             break



        looks_good(inputs)



    import os
    os.system("pause")

if __name__ == '__main__':
            main()
   

I am sure I can avoid the repetition in the code someway but my def(bb) doesnt work.

I am grateful for any help I can get with this.

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 <=0:
                print "Stake cannot be negative"
                stake = float(raw_input('Stake: '))

            odds = float(raw_input('Odds: '))
            while odds <=0:
                print "Odds cannot be negative"
                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: ')


# try get this into a function?

            def bb(book):
                while book in ['bfair','sky','wh','freds','pp']:
                    if result == 0:
                        book_balance = book_balance - stake
                        new_books.append(book_balance)
                    elif result == (stake * odds):
                        book_balance = book_balance - stake + (stake * odds)
                        new_books.append(book_balance)
                bb(bfair)
                bb(freds)
                bb(sky)
                bb(pp)
                bb(wh)


            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[1])
            print "Total Stake: %d" %sum(total_stake)

            total_winnings = []
            for my_list in inputs:
                total_winnings.append(my_list[3])
            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

        elif add_selection == "No":
             break



        looks_good(inputs)



    import os
    os.system("pause")

if __name__ == '__main__':
            main()

I hope someone can help me make this code better thank you.

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 <=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[1])
            print "Total Stake: %d" %sum(total_stake)

            total_winnings = []
            for my_list in inputs:
                total_winnings.append(my_list[3])
            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

        elif add_selection == "No":
             break



        looks_good(inputs)



    import os
    os.system("pause")

if __name__ == '__main__':
            main()
   
Post Closed as "Not suitable for this site" by Jamal
Source Link

Calculate total profit/loss

This program starts with initial balance and the individual book balances which must match the initial balance to proceed.

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.

I am sure I can avoid the repetition in the code someway but my def(bb) doesnt work.

I am grateful for any help I can get with this.

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 <=0:
                print "Stake cannot be negative"
                stake = float(raw_input('Stake: '))

            odds = float(raw_input('Odds: '))
            while odds <=0:
                print "Odds cannot be negative"
                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: ')


# try get this into a function?

            def bb(book):
                while book in ['bfair','sky','wh','freds','pp']:
                    if result == 0:
                        book_balance = book_balance - stake
                        new_books.append(book_balance)
                    elif result == (stake * odds):
                        book_balance = book_balance - stake + (stake * odds)
                        new_books.append(book_balance)
                bb(bfair)
                bb(freds)
                bb(sky)
                bb(pp)
                bb(wh)


            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[1])
            print "Total Stake: %d" %sum(total_stake)

            total_winnings = []
            for my_list in inputs:
                total_winnings.append(my_list[3])
            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

        elif add_selection == "No":
             break



        looks_good(inputs)



    import os
    os.system("pause")

if __name__ == '__main__':
            main()