Python Forum
I getting Index error after numerous tries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I getting Index error after numerous tries (/thread-17712.html)



I getting Index error after numerous tries - priyanshu - Apr-21-2019

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]


RE: I getting Index error after numerous tries - ichabod801 - Apr-21-2019

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.


RE: I getting Index error after numerous tries - SheeppOSU - Apr-21-2019

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


RE: I getting Index error after numerous tries - ichabod801 - Apr-21-2019

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.