讨论区 老师答疑区 主题详情
Rex教研机器学习大数据科学爬虫自动化方向
置顶 精华 老师参与 模特卡罗模拟与等价殃框架源代码与课程勘误

等价殃框架课程中源代码如下,课程中输的情况讨论中讲师打掉了一行将赌注重置的代码wager = initial_wager,完整源代码如下,欢迎各位同学探讨:

  1. import random
  2. import matplotlib.pyplot as plt
  3.  
  4.  
  5. def give_the_number():
  6.     roll = random.randint(1,100)
  7.  
  8.     if roll == 100:
  9.         return False
  10.     elif roll <= 50:
  11.         return False
  12.     elif 100 > roll >= 50:
  13.         return True
  14.  
  15.  
  16. def a_smart_fool(funds,initial_wager,game_count):
  17.     wager = initial_wager
  18.     = []
  19.     = []
  20.     current_game_count = 1
  21.  
  22.     previous_game = 'win'
  23.     previous_game_wager = initial_wager
  24.  
  25.     while current_game_count <= game_count:
  26.         if previous_game == 'win':
  27.             print('The fool win the last game˜yeah˜˜˜')
  28.             if give_the_number():
  29.                 funds += wager
  30.                 print(funds)
  31.                 x.append(current_game_count)
  32.                 y.append(funds)
  33.             else:
  34.                 funds -= wager  
  35.                 previous_game = 'loss'
  36.                 print(funds)
  37.                 previous_game_wager = wager
  38.                 x.append(current_game_count)
  39.                 y.append(funds)
  40.                 if funds < 0:
  41.                     print ('The fool went broke in'+
  42.                            str(current_game_count)+'games')
  43.                     break
  44.                  
  45.         elif previous_game == 'loss':
  46.             print("we lost the last one, but the fool will double up˜")
  47.             if give_the_number():
  48.                 wager = previous_game_wager * 2
  49.                 print ('we won'+str(wager))
  50.                 funds += wager
  51.                 print (funds)
  52.                 wager = initial_wager
  53.                 previous_game = 'win'
  54.                 x.append(current_game_count)
  55.                 y.append(funds)
  56.             else:
  57.                 wager = previous_game_wager * 2
  58.                 print ('we lost'+str(wager))
  59.                 funds -= wager
  60.                 if funds < 0:
  61.                     print('The fool went broke in'
  62.                           +str(current_game_count)+'games')
  63.                     break
  64.                 print(funds)
  65.                 previous_game = 'loss'
  66.                 previous_game_wager = wager
  67.                 x.append(current_game_count)
  68.                 y.append(funds)
  69.                 if funds < 0:
  70.                     print('went broke aftesr'+str(current_game_count)+'bets')
  71.                     break
  72.  
  73.         current_game_count += 1
  74.  
  75.     print(funds)
  76.     plt.plot(x,y)
  77.  
  78.  
  79. n=0
  80. while n<100:
  81.     a_smart_fool(10000,1000,100)
  82.     n+=1
  83.  
  84.  
  85.  
  86. plt.xlabel("How many games does the fool played")
  87. plt.ylabel("Funds")
  88. plt.show()

课程

所有回复(4)

置顶 精华

我想了半天这个代码,

60行和69行的if funds <0: 这里不是重复了吗?

而且69行写的str感觉是写错了?

求解答

2020-10-28

发表观点要登录哦!点击登录

置顶 精华

import random import matplotlib.pyplot as plt def random_give_number():    number = random.randint(1,100)    if number >51:        return True    else:        return False def a_fool(funds,initial_wager,game_count):    current_game_count =1    wager =initial_wager    previous_game_wager=initial_wager    previous_game =random.randint(0,1)    x=[]    y=[]    while current_game_count<=game_count:        if previous_game==1:            print('The fool win the last game,Play more')            if random_give_number():                funds +=wager                print(current_game_count,wager,funds)                x.append(current_game_count)                y.append(funds)            else:                funds -=wager                previous_game=0                print(current_game_count,wager,funds)                previous_game_wager=min(wager,funds)                x.append(current_game_count)                y.append(funds)                if funds <=0:                    print('The fool went broke in '+                          str(current_game_count)+'games')                    break        elif previous_game==0:            print('we lost the last game,but the fool will double up')            if random_give_number():                wager =min(previous_game_wager *2,funds)                print('we won'+str(wager))                funds +=wager                print(current_game_count,wager,funds)                previous_game=1                x.append(current_game_count)                y.append(funds)            else:                wager =min(previous_game_wager *2,funds)                print('we lost'+str(wager))                funds -=wager                if funds<=0:                    print('The fool went brok in '+                          str(current_game_count)+'games')                    break                print(current_game_count,funds)                previous_game=0                previous_game_wager=wager                x.append(current_game_count)                y.append(funds)                if funds<=0:                    print('We went borke after'+                          str(current_game_count)+'games')        current_game_count +=1        print(current_game_count,wager,funds)        plt.plot(x,y) a_fool(10000,1000,100) plt.xlabel('how many games does the fool play') plt.ylabel('funds') plt.show() 当投注金额大于本金时,应当取本金为当前投注金额,否则的话,会出现负数,与实际情况不符,所以改进了一下代码。
2019-7-30

发表观点要登录哦!点击登录

总感觉逻辑有点问题, previous_game = 'win'的情况下,这轮的本金应该是原来的本金加上投注才对,而上面的代码中只在这轮又赢了的情况下才将本金加了投注,

if give_the_number():

                funds += wager

这样的话就少算了上一轮赢了的投注,没有将上一轮赢得投注加到这一轮里来。

2019-12-31

老师,在前一次投注失败并且本次赢的情况下投注金额为什么要被赋值为初始投注金额,wager = initial_wager这句代码感觉很疑惑,不应该不需要这句吗

2019-8-13

发表观点要登录哦!点击登录

回复

发布

发表观点要登录哦!

最近活动:2020-10-28
创建时间:2019-7-10
浏览次数:1121
关注人数:0

使用协议与隐私政策

感谢您使用网易云课堂!

为了更好地保障您的个人权益,请认真阅读《使用协议》《隐私政策》《服务条款》的全部内容,同意并接受全部条款后开始使用我们的产品和服务。若不同意,将无法使用我们的产品和服务。

同意
手机课堂
下载App
返回顶部