Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Indices Error
#1
Hello,

I am currently working on a dummy project and I keep receiving on error message "List Indices Error" when I try to run the code:

print("Welcome to the Urban")

ab = {'J_Cole': 1, 'Drake': 2, 'Jay_Z': 3, 'Beyonce': 4, 'Kendrick_Lamar': 5}

input('''Please select your favorite artist:
            J_Cole
            Drake
            Jay_Z
            Beyonce
            Kendrick_Lamar''')

while True:
    ab = ['J_Cole', 'Drake',  'Jay_Z', 'Beyonce', 'Kendrick_Lamar']
    user_fav_artist = str(input('Select you favorite artist:'))
    user_fav_artist = ab

    if user_fav_artist == str(ab[0,4]): 
        print('Thank you for selecting')
        if str(input('Would you like to receive news updates? yes or no:')) == 'yes':
            continue
        else:
            print('You can subscribe later if you would like')
            break

    else:
        print('Your selection is invalid')
Reply
#2
The error is because ab[0,4] , ab is a list and you can only index a list with a single integer or a slice, which is what the error would have told you.
There are other problems with the code, see comments below.
print("Welcome to the Urban")
 
ab = {'J_Cole': 1, 'Drake': 2, 'Jay_Z': 3, 'Beyonce': 4, 'Kendrick_Lamar': 5} # this is not used, overwritten when ab reassigned
 
input('''Please select your favorite artist:
            J_Cole
            Drake
            Jay_Z
            Beyonce
            Kendrick_Lamar''') # result of this input not stored, manually have to change names
 
while True:
    ab = ['J_Cole', 'Drake',  'Jay_Z', 'Beyonce', 'Kendrick_Lamar'] # dictionary ab overwritten by this
    user_fav_artist = str(input('Select you favorite artist:')) # input returns str , string conversion not needed
    user_fav_artist = ab # user input lost reassigned to value of the list ab
 
    if user_fav_artist == str(ab[0,4]):  # if the index was valid it would return a string from the list, conversion not needed, user input not stored, wrong way of checking if an item is in a list.
        print('Thank you for selecting')
        if str(input('Would you like to receive news updates? yes or no:')) == 'yes': # conversion to string not required
            continue
        else:
            print('You can subscribe later if you would like')
            break # only breaks out of the loop if don't input 'Yes' is this the required action
 
    else:
        print('Your selection is invalid')
I will add some ways to improve your code

It would be easier if the artist strings where lower case, when it comes to comparison lower can be used on the users input.
if you want the displayed choice to show each word title cased you can use title.
users_input = 'J_cole'
print(users_input.lower())
value= 'j_cole'
print(value.title())
Output:
j_cole J_Cole
Rather than manually list the artists in the input, create a string from the list of artists
artists = ['j_cole', 'drake',  'jay_z', 'beyonce', 'kendrick_lamar']

artist_dispaly = '\n'.join(artist.title() for artist in artists)
user_input = input(f'Please selcet your favourite artist:\n{artist_dispaly}')
Output:
Please selcet your favourite artist: J_Cole Drake Jay_Z Beyonce Kendrick_Lama
To check if a valid artist from the list of artists has been input
user_input = 'Drake'
if user_input.lower() in artists:
    print(f'{user_input.title()} is in the list of artists')
Output:
Drake is in the list of artists
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,156 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,228 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,363 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,004 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,453 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,568 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  list indices must be integers or slices, not lists error djwilson0495 2 2,868 Aug-27-2020, 06:13 PM
Last Post: deanhystad
  [Solved]TypeError: list indices must be integers or slices, not str NectDz 3 3,916 Jun-02-2020, 08:21 AM
Last Post: DreamingInsanity
  TypeError: list indices must be integers or slices, not float hissonrr 2 3,308 Apr-19-2020, 12:02 AM
Last Post: hissonrr
  TypeError: list indices must be integers or slices, not str guilla25 1 3,394 Jan-08-2020, 11:20 AM
Last Post: buran

Forum Jump:

User Panel Messages

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