我试图使用while循环和递归函数在n个游戏上获得最小的赌注,但是当涉及到递归位时,似乎我的代码没有按照我的预期进行……任何帮助
我尝试在while循环中增加x变量之后尝试重新定义变量,然后将递归函数作为return Product()调用,但徒劳
我希望获得几行“重试”,而总赌注不同,最后获得一张门票清单,但我只得到了一行并且有一个空清单
from itertools import product
# from csv import writer
data = product(
(('home1', 1.53),( 'away1', 2.4)), #half time
(('home2', 1.53),( 'away2', 2.4)), #half time
(('home3', 1.653),( 'away3', 2.4)), #half time
(('home4', 1.653),( 'away4', 2.4)), #half time
(('home5', 1.653),( 'away5', 2.4)), #half time
)
def check(list1, val):
possible_return = []
for lst in list1:
possible_return.append(lst[-1])
# print(possible_return)
return all(i > val for i in possible_return)
def Product():
x = 1
while True:
dict_of_tickets = {}
list_of_tickets = []
total_Stake = 0
list_of_Odds =[]
pay_out_list = []
for ticket in data:
TOTAL_ODDS = 1
n = 2 ** len(ticket)
for i in ticket:
TOTAL_ODDS = round((TOTAL_ODDS * i[1]), 3)
list_of_Odds.append(TOTAL_ODDS)
odd_per_ticket = TOTAL_ODDS
if odd_per_ticket <= n:
stake = ((n / odd_per_ticket)*x)
pay_out = round(((odd_per_ticket+odd_per_ticket/4)*stake),0)
pay_out_list.append(pay_out)
total_Stake += stake
reciept = [ticket, odd_per_ticket, stake, pay_out]
list_of_tickets.append(reciept)
else:
stake = x/2
pay_out = round(((odd_per_ticket+odd_per_ticket/4)*stake),0)
pay_out_list.append(pay_out)
total_Stake += stake
reciept = [ticket, odd_per_ticket, stake, pay_out]
list_of_tickets.append(reciept)
# print(ticket)
# print('==========================================================')
# print(list_of_tickets)
if check(list_of_tickets, total_Stake):
return list_of_tickets
else:
print("Trying again....!", total_Stake)
x *= 1.5
dict_of_tickets = {}
list_of_tickets = []
total_Stake = 0
list_of_Odds =[]
pay_out_list = []
return Product()
Product()
答案 0 :(得分:0)
我希望获得几行“重试”,而总赌注不同,最后获得一张门票清单,但我只得到了一行并且有一个空清单
发生这种情况是因为data
是生成器。 This answer是了解迭代器和生成器之间区别的好资源。
简而言之,您只能在生成器上循环一次。这就是为什么您只得到'Trying again'
一行的原因。
一种可能的解决方案是使数据成为迭代器,例如一个列表。
data = list(product(
(('home1', 1.53),( 'away1', 2.4)), #half time
(('home2', 1.53),( 'away2', 2.4)), #half time
(('home3', 1.653),( 'away3', 2.4)), #half time
(('home4', 1.653),( 'away4', 2.4)), #half time
(('home5', 1.653),( 'away5', 2.4)), #half time
))
通过这种方式,您可以随时循环播放它。
这就是说,我已通过此修复程序尝试过您的代码,但由于数字if check(list_of_tickets, total_Stake):
始终被评估为False
,所以它以无限循环结束,数值迅速增长到无限。
您需要检查逻辑(所有计算和检查工作)是否正确完成,我不确定问题出在哪里。
最后的注释,要打印最终结果(一旦解决了无限循环问题),请记住将函数的结果保存在变量中并打印出来。做:
lot = Product()
print(lot)
而不是简单地调用Product()
。