我正在尝试创建一个 Martingale 投注模拟器。我的全局“running_profit”变量应该由“change_running_total”函数间接更新。running_total 变量已正确更新,但受 running_total 影响的 running_profit 变量未更新。import randomround_n = 0bet = 25starting_funds = 5000running_total = starting_fundsrunning_profit = running_total - starting_fundsgoal_profit = 5000wins = 0losses = 0rng_v = 0.00def rng(): """Generates random number""" global rng_v rng_v = random.random()def winloss_generator(): """Generates win/loss condition""" global rng_v if rng_v <= .49: return 'win' else: return 'loss'def increase_winloss(): """Increases wins or losses variable based on winloss_generator output""" global wins global losses if winloss_generator() == 'win': wins += 1 return 'win' elif winloss_generator() == 'loss': losses += 1 return 'loss' else: print('error')def increase_round(): """Increases round number by 1""" global round_n round_n += 1 print('round: ', round_n)def change_running_total(): """Increases or decreases running_total based on winloss_generator""" global running_total if winloss_generator() == 'win': running_total += bet elif winloss_generator() == 'loss': running_total -= bet else: print('error')def print_current_record(): """Prints current win/loss record to screen""" print('Current Record: ', wins, 'wins, ', losses, 'losses')def print_winloss(): """Prints win/loss condition to screen""" print(winloss_generator(), 'print_winloss'.upper())def print_profit(): """Print running profit to screen""" print(running_profit, 'print_profit (variable)'.upper()) print(running_total - starting_funds, 'print_profit (calculated)'.upper())def return_current_funds(): """Returns running total of funds""" global running_total return running_total
1 回答
阿晨1998
TA贡献2037条经验 获得超6个赞
您在顶部声明变量:
running_profit = running_total - starting_funds
然而,这条线路只运行一次;您没有在这里建立某种关系,只要您更改running_total或 ,它就会自动更新starting_funds。如果你想这样做,请将其变成一个函数:
def running_profit():
global running_total, starting_funds
return running_total - starting_funds
现在,每次您调用时running_profit(),它都会根据这些变量的当前值进行计算。
旁注,与其使用全局变量来完成所有这些操作,不如将其封装到一个类中。
添加回答
举报
0/150
提交
取消