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?
#4
I was playing around with it. Look at line 56. Notice the indentation. That's wrong. Fix it.

Hint: add some print statements to your code. When I printed out the value, it was [n] where n was the last number I had typed. What happened to the others? Well, you wrote the wrong code.

To improve the user experience, I changed the input line to be
y = int(input("Enter Leaf Value "+ str(i + 1) + "/" + str(x) + ": "))
and I added this line just before your line 10 (the if depth == 3 line)
print("minmax(depth=", depth, ", nodeindex=", nodeIndex, ", ", maximizingPlayer, ", ", values, ", ", alpha, ", ", beta, ")"
    )
I still get errors because I have no idea what the purpose of the code is, or what it is working on, but it is clear where your basic error is. Fix that (I did in mine). Here's my output:
Output:
Enter total number of leaf nodes=4 Enter Leaf Value 1/4: 11 Enter Leaf Value 2/4: 12 Enter Leaf Value 3/4: 13 Enter Leaf Value 4/4: 14 minmax(depth= 0 , nodeindex= 0 , True , [11, 12, 13, 14] , -1000 , 1000 ) minmax(depth= 1 , nodeindex= 0 , False , [11, 12, 13, 14] , -1000 , 1000 ) minmax(depth= 2 , nodeindex= 0 , True , [11, 12, 13, 14] , -1000 , 1000 ) minmax(depth= 3 , nodeindex= 0 , False , [11, 12, 13, 14] , -1000 , 1000 ) minmax(depth= 3 , nodeindex= 1 , False , [11, 12, 13, 14] , 11 , 1000 ) minmax(depth= 2 , nodeindex= 1 , True , [11, 12, 13, 14] , -1000 , 12 ) minmax(depth= 3 , nodeindex= 2 , False , [11, 12, 13, 14] , -1000 , 12 ) minmax(depth= 1 , nodeindex= 1 , False , [11, 12, 13, 14] , 12 , 1000 ) minmax(depth= 2 , nodeindex= 2 , True , [11, 12, 13, 14] , 12 , 1000 ) minmax(depth= 3 , nodeindex= 4 , False , [11, 12, 13, 14] , 12 , 1000 )
The last printout shows a node index of 4, which is larger than any valid node index (1..3) of your array of values.
If you really want help, you have to tell us what values you typed in, so we can reproduce your experience. And it is worthwhile to explain what the purpose of the code is, so we know what it should do. It wouldn't hurt if you said "I expected the answer to be <answer value here>, but instead got an exception" so we know what was expected. You might indicate that the nodes form a tree structure, and explain how the index relates to the tree structure.
Reply


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

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