Python Forum
list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list index out of range
#1
The python code isnt working because the loop i think isnt doing the things in the right order. Been trying to fix this for 4hours and want to know why it is not working. Help would be appreciated
Yoriz write Aug-31-2021, 05:33 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button

Attached Files

.py   Summer coding project.py (Size: 914 bytes / Downloads: 249)
Reply
#2
You should include your code in your post, not as a link. You should also post the error message including the entire traceback. You should also provide some example input and any output

You've been looking at this for 4 hours. What have you tried to better understand the reason for the error?

If you want to loop over the strings in Q1, why not do this?
for word in Q1.rsplit():
    if word in issues:
        print("We have identified the problem")
        break
else # Didn't find word in issues
    humandata = input("We could not find an automated solution for you, please type as much infomation about the issue and we will let a technician sort it out")
    cases += 1
    new_issues.write(str(cases))
    new_issues.write(humandata)
Reply
#3
Got it working nvm
Reply
#4
There are several ways to resolve indexerror in Python. It is not recommended to access the list or an array in any programming by using a hardcoded index value. It often leads to an exception, instead use one of the below best practices.
  • Use len() function before accessing list
    Use for loop with ‘in’ to print out list elements
    for loop with range() is better way to even access list elements and print them

Checkout an article list index out of range for more details

numbers = [1, 2, 3, 4, 6, 8, 10]
index = 3
if index < len(numbers):
    print(numbers[index])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 5,972 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  Index List a04j 2 2,932 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  List index out of range when turning CSV into dict ranbarr 15 6,471 May-12-2021, 10:38 AM
Last Post: ranbarr
  List vs index: Frederico_Caldas 5 3,612 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,269 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  list index out of range mcgrim 2 2,916 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 3,864 Jan-17-2019, 07:54 AM
Last Post: buran
  String index out of range felie04 2 5,535 Aug-17-2018, 11:18 PM
Last Post: felie04
  Accessing data in zip - Index out of range pythoneer 24 12,785 Mar-15-2018, 06:19 PM
Last Post: buran
  "List index out of range" for output values pegn305 3 5,309 Nov-26-2017, 02:20 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

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