0

I'm going through a text on elementary probability theory, http://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/amsbook.mac.pdf, as a refresher and using it as an opportunity to teach myself some programming.

I'm trying to write a simulation for a Labouchere betting system in roulette (question 9 page# 13 in the linked pdf).

I think that I am close to the right answer but I can't figure out how to get the list 'bet' to change for each successive round.

Here is what I have so far:

    def Labouchere(T):
        bet, mon = [4,3,2,1], 0
        for t in range(0,T):
            ball = random.uniform(0.0,1.0)
            if ball < (18/float(38)):
                mon += (bet[0] + bet[-1])
                np.delete(bet, 0, 0)
                np.delete(bet, -1, 0)
            elif ball >= (18/float(38)):
                mon -= (bet[0] + bet[-1])
                np.insert(bet, 0, (bet[0]+bet[-1]))
            else:
                break
        return mon

Any help is much appreciated. I am also trying to learn from Steven F. Lott's book 'Building Skills in Object-Oriented Design' but it seems to be above my level of understanding so far. A recommendation for material that would prep me for using that guide would also be invaluable.

Thanks in advance.

pauld
  • 401
  • 1
  • 5
  • 20
  • What exactly are you trying to change in the list? Are you just replacing elements, or do you want to change its length? – jonrsharpe Jul 21 '14 at 15:40
  • I want to change its length. For Labouchere, you have a list ex [1,2,3,4]. You bet the sum of the first and last element in that list. If you win, you delete those elements from the list and then repeat. If you lose the bet, then you add that sum to the end of the list and then repeat. After each bet the list will be different: Ex. initial list is [1,2,3,4] win -> list = [2,3], lose -> list = [1,2,3,4,5] – pauld Jul 21 '14 at 15:45

2 Answers2

1

In the winning case, either:

bet = bet[1:-1] # "slice" from second to penultimate

or:

bet.pop(0) # remove first
bet.pop(-1) # remove last

In your losing case:

bet.append(mon) # add to end
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

You can use slicing and append to achieve this. See Explain Python's slice notation for more details.

If the user wins:

list = list[1:len(list)-1]

If the user loses:

list.append(list[0] + list[len(list)-1])
Community
  • 1
  • 1
Cytomat
  • 5
  • 2