0

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.

Black Magic

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

Eddie Baker
  • 21
  • 1
  • 5
  • 1
    Define *breaking*. You need to give a clear problem-statement, rather than expect your code to be debugged for you. Also, please don't link to screenshots of a text display - paste the output directly into the question, and use formatting to display it properly. – SiHa Sep 15 '16 at 12:06
  • One element is removed from the list. First time at index 1 and then at index 0. What is so impossible? – ElmoVanKielmo Sep 15 '16 at 12:41
  • The list Last_current, its printed after the only edit but then changes when it is printed later? – Eddie Baker Sep 15 '16 at 12:58
  • `last_current = current` doesn't create a copy of list. Both variables reference the same list. Modifications inside `box()` function affect `last_current` variable. – ElmoVanKielmo Sep 15 '16 at 13:21

0 Answers0