简体   繁体   English

如何在for循环中更改列表? Python中的Labouchere

[英]How to change a list in a for loop? Labouchere in Python

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. 我正在阅读有关基本概率论的文章, http://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/amsbook.mac.pdf ,作为复习课程,并以此为契机自学了一些节目。

I'm trying to write a simulation for a Labouchere betting system in roulette (question 9 page# 13 in the linked pdf). 我正在尝试通过轮盘赌(链接pdf中的问题9第13页的问题)为Labouchere投注系统编写模拟。

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. 我还尝试从Steven F. Lott的书“面向对象设计中的构建技能”中学习,但是到目前为止,这似乎超出了我的理解水平。 A recommendation for material that would prep me for using that guide would also be invaluable. 推荐给我一些使用该指南的材料也是非常宝贵的。

Thanks in advance. 提前致谢。

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

You can use slicing and append to achieve this. 您可以使用切片和附加来实现此目的。 See Explain Python's slice notation for more details. 有关更多详细信息,请参见解释Python的切片符号

If the user wins: 如果用户获胜:

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

If the user loses: 如果用户失败:

list.append(list[0] + list[len(list)-1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM