Python Forum
How to resolve Index Error in my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to resolve Index Error in my code?
#7
That helps a lot. I also added statements to print the result on each return statement, and got
Output:
Enter total number of leaf nodes=8 Enter Leaf Value 1/8: 3 Enter Leaf Value 2/8: 5 Enter Leaf Value 3/8: 6 Enter Leaf Value 4/8: 9 Enter Leaf Value 5/8: 1 Enter Leaf Value 6/8: 2 Enter Leaf Value 7/8: 0 Enter Leaf Value 8/8: -1 minimax(depth= 0 , nodeindex= 0 , True , [3, 5, 6, 9, 1, 2, 0, -1] , -1000 , 1000 ) .minimax(depth= 1 , nodeindex= 0 , False , [3, 5, 6, 9, 1, 2, 0, -1] , -1000 , 1000 ) ..minimax(depth= 2 , nodeindex= 0 , True , [3, 5, 6, 9, 1, 2, 0, -1] , -1000 , 1000 ) ...minimax(depth= 3 , nodeindex= 0 , False , [3, 5, 6, 9, 1, 2, 0, -1] , -1000 , 1000 ) ... => [ 0 ] 3 ...minimax(depth= 3 , nodeindex= 1 , False , [3, 5, 6, 9, 1, 2, 0, -1] , 3 , 1000 ) ... => [ 1 ] 5 .. => 5 ..minimax(depth= 2 , nodeindex= 1 , True , [3, 5, 6, 9, 1, 2, 0, -1] , -1000 , 5 ) ...minimax(depth= 3 , nodeindex= 2 , False , [3, 5, 6, 9, 1, 2, 0, -1] , -1000 , 5 ) ... => [ 2 ] 6 .. => 6 . => 5 .minimax(depth= 1 , nodeindex= 1 , False , [3, 5, 6, 9, 1, 2, 0, -1] , 5 , 1000 ) ..minimax(depth= 2 , nodeindex= 2 , True , [3, 5, 6, 9, 1, 2, 0, -1] , 5 , 1000 ) ...minimax(depth= 3 , nodeindex= 4 , False , [3, 5, 6, 9, 1, 2, 0, -1] , 5 , 1000 ) ... => [ 4 ] 1 ...minimax(depth= 3 , nodeindex= 5 , False , [3, 5, 6, 9, 1, 2, 0, -1] , 5 , 1000 ) ... => [ 5 ] 2 .. => 2 . => 2 => 5 The optimal value is : 5
This does not correspond to your output, but since I made no real changes in the code, I'm putting the code here:
MAX, MIN = 1000, -1000
 
def indent(depth):
    for i in range(depth):
       print(".",sep="", end="")

# Returns optimal value for current player
# (Initially called for root and maximizer)
def minimax(depth, nodeIndex, maximizingPlayer,
            values, alpha, beta):

    indent(depth)
    print("minimax(depth=", depth, ", nodeindex=", nodeIndex, ", ", maximizingPlayer, ", ", values, ", ", alpha, ", ", beta, ")"
    )

    # Terminating condition. i.e
    # leaf node is reached
    if depth == 3:
        indent(depth)
        print(" => [", nodeIndex, "] ", values[nodeIndex])
        return values[nodeIndex]
 
    if maximizingPlayer:
 
        best = MIN
 
        # Recur for left and right children
        for i in range(0, 2):
 
            val = minimax(depth + 1, nodeIndex * 2 + i,
                          False, values, alpha, beta)
            best = max(best, val)
            alpha = max(alpha, best)
 
            # Alpha Beta Pruning
            if beta <= alpha:
                break
        indent(depth)
        print(" => ", best)
        return best
 
    else:
        best = MAX
 
        # Recur for left and
        # right children
        for i in range(0, 2):
 
            val = minimax(depth + 1, nodeIndex * 2 + i,True, values, alpha, beta)
            best = min(best, val)
            beta = min(beta, best)
 
            # Alpha Beta Pruning
            if beta <= alpha:
                break
        indent(depth)
        print(" => ", best)
        return best
 
    # Driver Code
 
 
if __name__ == "__main__":
    scr = []  # List for Leaf Nodes
    x = int(input("Enter total number of leaf nodes="))
    for i in range(0,x):
        y = int(input("Enter Leaf Value "+ str(i + 1) + "/" + str(x) + ": "))
        scr.append(y)
 
 
 
    print("The optimal value is :", minimax(0, 0, True, scr, MIN, MAX))
I do not understand why MIN and MAX are global variables, but I didn't try to debug the code. Looking at the output, perhaps you can figure out why it does not deliver the same result as you indicated that it should. I am very suspect of your use of MIN and MAX which you never change. But that's just a guess.

The key takeaway here is the use of print statements to tell you what is really happening.
Reply


Messages In This Thread
RE: How to resolve Index Error in my code? - by supuflounder - May-22-2021, 11:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 999 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 776 May-15-2023, 03:28 PM
Last Post: buran
  [split] How to resolve version conflicts in Python? atonalwilson 1 1,005 May-04-2023, 09:02 AM
Last Post: buran
  How to resolve version conflicts in Python? taeefnajib 0 936 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  How to resolve my problem in Pycharm? bshoushtarian 0 865 Sep-26-2022, 11:45 AM
Last Post: bshoushtarian
  I'm getting a String index out of range error debian77 7 2,361 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,987 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  Python Error List Index Out of Range abhi1vaishnav 3 2,325 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,251 Aug-18-2021, 05:27 AM
Last Post: snippsat
  Index error - columns vs non-column Vinny 3 4,937 Aug-09-2021, 04:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020