Home > Net >  Why does it give me IndexError: list index out of range for this piece of code? I double-checked
Why does it give me IndexError: list index out of range for this piece of code? I double-checked

Time:01-21

Help I tried running this piece of code yet it keeps saying list index out of range. Whenever I go to the last list item it just gives that error. Everything else works fine. What am I doing wrong and how do I fix it? '

conversations = ['favouriteFood', 'anyPets', 'idealGift', 'favouriteShow']


def conversationStarter():

        
    if conversations[1] == 'favouriteFood':
        conversations.remove('favouriteFood')
        answer1 = input('What is your favourite food? My favourite food is codewords.')
        answer2 = input('Wow! I love eating '   answer1.lower()   ' too! What is your second favourite?')
        print('Really? I never really learnt to like '   answer2.lower()   ', but I will have a go!')
        conversationStarter()
    elif conversations[1] == 'anyPets':
        conversations.remove('anyPets')
        answer = input('Hey hey hey! Do you have any pets? I do, of course, a pet python! Do you have any pets?')
        if answer == 'yes':
            answer = input('Really! What kind of pet do you own?')
            print('Wow! I would love to have a '   answer.lower()   ' someday!')
            conversationStarter()
        if answer == 'no':
            print('Oh. OK! However, I highly recommend a pet, they make you happier!')
            conversationStarter()
    elif conversations[1] == 'idealGift':
        conversations.remove('idealGift')
        answer = input('What is your most ideal gift for the New Year?')
        print('WHAT? You think of a '   answer.lower()   ' as an ideal gift? I would rather like a Python code.')
        conversationStarter()
    elif conversations[1] == 'favouriteShow':
        conversations.remove('favouriteShow')
        answer = input('What is your favourite movie or TV film?')
        print('Wait what? I never heard of '   answer   '. I should watch it! I love watching Python tutorials though.')
        conversationStarter()

def greetingMenu():
    print('Welcome to the NewYearPyChat.home file. Do you want a conversation?')
    answer = input("Please answer 'yes' or 'no'.")
    if answer == 'yes':
        print('Amazing! Let us start chatting then!')
        conversationStarter()
    elif answer == 'no':
        print('Oh. Try again next time.')
        greetingMenu()
    else:
        print('What are you saying?')
        greetingMenu()


        
    

greetingMenu()
print('And by the way, my name is PyChat if you have not guessed already.')

I tried looking at the piece of code and fixing bits and deleting nearly all of it but it still doesn't work.

CodePudding user response:

You are referencing the second element of the array in each of your if-elif checks. Change each instance of conversations[1] to conversations[0] and you should see the results you're looking for.

CodePudding user response:

As @Barmar said, you are removing elements in a list and it throws a error when the list is empty. instead you can use for loop

conversations = ['favouriteFood', 'anyPets', 'idealGift', 'favouriteShow']


def conversationStarter():
    for i in conversations:
        if i == 'favouriteFood':
            conversations.remove('favouriteFood')
            answer1 = input('What is your favourite food? My favourite food is codewords.')
            answer2 = input('Wow! I love eating '   answer1.lower()   ' too! What is your second favourite?')
            print('Really? I never really learnt to like '   answer2.lower()   ', but I will have a go!')
            conversationStarter()
        elif i == 'anyPets':
            conversations.remove('anyPets')
            answer = input('Hey hey hey! Do you have any pets? I do, of course, a pet python! Do you have any pets?')
            if answer == 'yes':
                answer = input('Really! What kind of pet do you own?')
                print('Wow! I would love to have a '   answer.lower()   ' someday!')
                conversationStarter()
            if answer == 'no':
                print('Oh. OK! However, I highly recommend a pet, they make you happier!')
                conversationStarter()
        elif i == 'idealGift':
            conversations.remove('idealGift')
            answer = input('What is your most ideal gift for the New Year?')
            print('WHAT? You think of a '   answer.lower()   ' as an ideal gift? I would rather like a Python code.')
            conversationStarter()
        elif i == 'favouriteShow':
            conversations.remove('favouriteShow')
            answer = input('What is your favourite movie or TV film?')
            print(
                'Wait what? I never heard of '   answer   '. I should watch it! I love watching Python tutorials though.')
            conversationStarter()


def greetingMenu():
    print('Welcome to the NewYearPyChat.home file. Do you want a conversation?')
    answer = input("Please answer 'yes' or 'no'.")
    if answer == 'yes':
        print('Amazing! Let us start chatting then!')
        conversationStarter()
    elif answer == 'no':
        print('Oh. Try again next time.')
        greetingMenu()
    else:
        print('What are you saying?')
        greetingMenu()


greetingMenu()
print('And by the way, my name is PyChat if you have not guessed already.')
  • Related