UPDATE: I've identified that the bug is happening when the try function fails, I guess if i modify the code to not use a try and the box function to return false if it doesn't work it might fix the problem
So i'm trying to work out the probability of winning at shut the box http://www.mastersgames.com/rules/shut-box-rules.htm
I've got it mostly working, so a function that closes the "slots" using the biggest number possible strategy. But now i'm trying to get it to work it out for every combination of dice roll sequence using recursion.
PROBLEM: The list last_current is only ever edited at point labelled 1, and it is then printed with the tag "LCC", however later when it is printed with the tag "LC", at point 2, the first element from the list has been removed.
global Total_Count
Total_Count = 0
Shut_Count = 0
Startlist = ["1","2","3","4","5","6","7","8","9"]
shutting = []
def box(n,current):
if str(n) in current:
current.remove(str(n))
for i in range(len(shutting)):
current.remove(shutting[i])
shutting.pop()
return current
else:
for i in range(n):
if str(n-i) in current:
shutting.append(str(n-i))
return box(i,current)
break
else:
pass
def recurs(Dice_1,Dice_2,current):
global Total_Count
for i in range(Dice_1):
for j in range(Dice_2):
print(i,j,current)
1) last_current = current
print("LCC",last_current)
try:
n = i + j + 2
print(n,current)
new = box(n,current)
current = new
print("Try",current)
except:
2) print("LC",last_current)
current = last_current
Total_Count += 1
print("Total",Total_Count,"Current",current)
else:
#print(current)
if range(len(current)) == 0:
Total_Count += 1
Shut_Count += 1
print("Total",Total_Count,"Shut",Shut_Count)
else:
recurs(Dice_1,Dice_2,current)
recurs(6,6,Startlist)
print(Total_Count,Shut_Count)
Ignore the terrible coding and use of breaks, also i don't want someone elses solution to the problem just the solution to why the code is breaking