Python Forum

Full Version: List Indices Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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')
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