Python Forum
I getting Index error after numerous tries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I getting Index error after numerous tries
#1
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]
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 739 May-15-2023, 03:28 PM
Last Post: buran
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 4,849 Aug-09-2021, 04:46 PM
Last Post: snippsat
  How to resolve Index Error in my code? codify110 6 2,957 May-22-2021, 11:04 AM
Last Post: supuflounder
  index error surim 4 2,459 Dec-05-2020, 02:34 PM
Last Post: deanhystad
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,013 Nov-13-2020, 07:43 AM
Last Post: buran
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 4,957 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  List index out of range error while accessing 2 lists in python K11 2 2,061 Sep-29-2020, 05:24 AM
Last Post: K11

Forum Jump:

User Panel Messages

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