等价殃框架课程中源代码如下,课程中输的情况讨论中讲师打掉了一行将赌注重置的代码wager = initial_wager,完整源代码如下,欢迎各位同学探讨:
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()
当投注金额大于本金时,应当取本金为当前投注金额,否则的话,会出现负数,与实际情况不符,所以改进了一下代码。
发表观点要登录哦!点击登录
发表观点要登录哦!点击登录
我也来评论