Python Forum

Full Version: I getting Index error after numerous tries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have been trying to make a user preference type project, so to make some bare bones I tried this..[See the pic]
I don't know why am I getting index error, Its very random Sometimes I get in 2 to 3 times and sometimes after getting the input couple of time(10-12)
Please Help me I am a beginner.[Image: LIltXyo]
You can't post images. We don't like images anyway. Please copy and paste the relevant code (in Python tags) and the full text of the error you are getting.
I'll post the code for the person -
from random import randint
positive = ['a', 'e', 'i', 'o', 'u']
negative = ['b', 'c', 'd', 'f', 'j', 'k', 'l']
negative_comments = ['b', 'c', 'd', 'f', 'j', 'k', 'l']
positive_comments = ['a', 'e', 'i', 'o', 'u']
x = ''
print(len(negative_comments))
while x != 'exit':
    x = input('>')
    if x in negative:
        print(negative_comments[randint(0, len(negative_comments))])
    if x in positive:
        print(positive_comments[randint(0, len(positive_comments))])
Sorry if I made a mistake when copying the code over

It gives me no errors
You should really be using random.choice on line 11 and 13, as in print(random.choice(negative_comments)). Much simpler and less error prone.