0

am trying to get the minimum stake on n games using a while loop and recursion function, but it seems like my code is not proceeding like i intended when it comes to recursion bit....any help

i tried redefining the variables after increasing the x variable in the while loop before calling the recursion function as return Product() but in vain

i expected to get several lines of 'Trying again' with a different total stake then at last a list of tickets, but instead i get only only one line and an empty list

from itertools import product
# from csv import writer

data = product(
        (('home1', 1.53),( 'away1', 2.4)), #half time
        (('home2', 1.53),( 'away2', 2.4)), #half time
        (('home3', 1.653),( 'away3', 2.4)), #half time
        (('home4', 1.653),( 'away4', 2.4)), #half time
        (('home5', 1.653),( 'away5', 2.4)), #half time
            )

def check(list1, val):
    possible_return = []
    for lst in list1:
        possible_return.append(lst[-1])
    # print(possible_return)
    return all(i > val for i in possible_return)

def Product():
    x = 1
    while True:
        dict_of_tickets = {}
        list_of_tickets = []
        total_Stake = 0
        list_of_Odds =[]
        pay_out_list = []
        for ticket in data:
            TOTAL_ODDS = 1
            n = 2 ** len(ticket)
            for i in ticket:
                TOTAL_ODDS = round((TOTAL_ODDS * i[1]), 3)
                list_of_Odds.append(TOTAL_ODDS)
            odd_per_ticket = TOTAL_ODDS
            if odd_per_ticket <= n:
                stake = ((n / odd_per_ticket)*x)
                pay_out = round(((odd_per_ticket+odd_per_ticket/4)*stake),0)
                pay_out_list.append(pay_out)
                total_Stake += stake
                reciept = [ticket, odd_per_ticket, stake, pay_out]
                list_of_tickets.append(reciept)
            else:
                stake = x/2
                pay_out = round(((odd_per_ticket+odd_per_ticket/4)*stake),0)
                pay_out_list.append(pay_out)
                total_Stake += stake
                reciept = [ticket, odd_per_ticket, stake, pay_out]
                list_of_tickets.append(reciept)

                # print(ticket)
        # print('==========================================================')
        # print(list_of_tickets)

        if check(list_of_tickets, total_Stake):
            return list_of_tickets
        else:
            print("Trying again....!", total_Stake)
        x *= 1.5
        dict_of_tickets = {}
        list_of_tickets = []
        total_Stake = 0
        list_of_Odds =[]
        pay_out_list = []
    return Product()
Product()
mad
  • 1

1 Answers1

0

i expected to get several lines of 'Trying again' with a different total stake then at last a list of tickets, but instead i get only only one line and an empty list

This happens because data is a generator. This answer is a great resource to understand the difference between an iterator and a generator.
In short, you can loop over a generator only once. That's why you get only one line of 'Trying again'.

A possible solution is to make data an iterator, a list for example.

data = list(product(
        (('home1', 1.53),( 'away1', 2.4)), #half time
        (('home2', 1.53),( 'away2', 2.4)), #half time
        (('home3', 1.653),( 'away3', 2.4)), #half time
        (('home4', 1.653),( 'away4', 2.4)), #half time
        (('home5', 1.653),( 'away5', 2.4)), #half time
            ))

This way you can loop over it all the times you want.

This said, I tried your code with this fix, but it ends in a infinite loop with numbers growing quite fast to infinite, because this if check(list_of_tickets, total_Stake): is always evaluated to False.
You need to check that your logic (all calculations and checks you are doing) are done properly, I am not sure where the problem is.

Final note, to print the final result (once you fix the problem with the infinite loop) remember to save the result of the function in a variable and print it. Do:

lot = Product()
print(lot)

instead of simply calling Product().

Valentino
  • 7,291
  • 6
  • 18
  • 34