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?
#1
This is the code
MAX, MIN = 1000, -1000


# Returns optimal value for current player
# (Initially called for root and maximizer)
def minimax(depth, nodeIndex, maximizingPlayer,
            values, alpha, beta):
    # Terminating condition. i.e
    # leaf node is reached
    if depth == 3:
        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

        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

        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: "))
    scr.append(y)



    print("The optimal value is :", minimax(0, 0, True, scr, MIN, MAX))
I am just trying to modify this code :
https://www.geeksforgeeks.org/minimax-al...a-pruning/
by allowing user to enter the leaf nodes itself.
But I am getting this error : File "C:\Users\Hp\PycharmProjects\lab4\Hilclimbing.py", line 15, in minimax return values[nodeIndex]
IndexError: list index out of range
Reply


Messages In This Thread
How to resolve Index Error in my code? - by codify110 - May-21-2021, 11:02 PM

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 778 May-15-2023, 03:28 PM
Last Post: buran
  [split] How to resolve version conflicts in Python? atonalwilson 1 1,007 May-04-2023, 09:02 AM
Last Post: buran
  How to resolve version conflicts in Python? taeefnajib 0 940 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  How to resolve my problem in Pycharm? bshoushtarian 0 867 Sep-26-2022, 11:45 AM
Last Post: bshoushtarian
  I'm getting a String index out of range error debian77 7 2,363 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,990 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  Python Error List Index Out of Range abhi1vaishnav 3 2,326 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,253 Aug-18-2021, 05:27 AM
Last Post: snippsat
  Index error - columns vs non-column Vinny 3 4,939 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