-1

i can enter inputs for one go through. It gives me the index error after entering my second lastNames input. Why is it doing this? index should be at 1 for this. The range is 15, why is it doing this? how do i fix it?

code is below:

#Purpose: Take in 15 inputs for a trainers last name and the amount of new members they 
enrolled, then output how many trainers got a certain indicated amount of enrollees.
LIMIT = 15
lastNames = [LIMIT]
enrollees = [LIMIT]
index = 0
membersZeroFive = 0
membersSixTen = 0
membersElevenFifteen = 0



while index < 16 :
  lastNames[index] = input("Enter your last name, trainer. \n")
  enrollees[index] = input("How many new members were you able to enroll? \n")
  if enrollees[index] == '0' or '1' or '2' or '3' or '4' or '5' :
    membersZeroFive = membersZeroFive+1 
  if enrollees[index] == '6' or '7' or '8' or '9' or '10' :
    membersSixTen = membersSixTen+1
  if enrollees[index] == '11' or '12' or '13' or '14' or '15' :
    membersElevenFifteen = membersElevenFifteen+1
  else :
    print("your number is either above 15 or negative. I can not operate with such values. Please take note and run the program again.\n")
  index = index+1


print("Trainers who got 0-5 members: " + membersZeroFive)
print("Trainers who got 6-10 members: " + membersSixTen)
print("Trainers who got 11-15 members: " + membersElevenFifteen)
Ian Schaak
  • 23
  • 2
  • Why do you expect `lastNames[index]` to work when `index > 0`? `lastNames` contains a single element as you defined it. Did you mean to _append_ to the list instead? – Pranav Hosangadi May 04 '22 at 18:59
  • @Karl that's not a duplicate for this question (although it's another issue that OP hasn't yet discovered). This question asks about the `IndexError` that occurs when OP attempts to set `lastNames[1]` – Pranav Hosangadi May 04 '22 at 19:00
  • 1
    Welcome to Stack Overflow. There are many issues with this code, and the general approach does not make sense. For one, please see https://stackoverflow.com/questions/20002503/. For another, `lastNames = [LIMIT]` **does not** make a list with 15 elements in it. It makes a list with **one** element in it, which is the number `15`. (Do you understand why? Do you see why this causes a problem?) – Karl Knechtel May 04 '22 at 19:01
  • @PranavHosangadi I was hasty, and reverted it. Now I cannot re-close with the duplicate for the actually asked question. That said, this question is probably not a useful duplicate link. – Karl Knechtel May 04 '22 at 19:02
  • Also note that `enrollees[index] == '0' or '1' or '2' or '3' or '4' or '5'` isn't going to give you the result you expect, as Karl noted. You'd be much better off [converting your input to a number](https://stackoverflow.com/q/20449427/843953) and checking if `0 <= enrollees[index] <= 5`. – Pranav Hosangadi May 04 '22 at 19:04
  • @Karl IDK if there's a good duplicate for this question. Maybe the link from Jony's answer below https://stackoverflow.com/questions/10712002/create-an-empty-list-in-python-with-certain-size but then my preferred solution would be to _append_ to the list instead of set the element. – Pranav Hosangadi May 04 '22 at 19:05
  • this is all really helpful! thank you so much! – Ian Schaak May 04 '22 at 19:15

1 Answers1

0

lastNames = enrollees = [15]

Your assignation is incorrect, only have a list of size 1. I think, that's the reason of your error. You have to create a list with size of 15.

This is a question that can help you: Create an empty list in Python with certain size

Jony_23
  • 322
  • 4
  • 12
  • i really appreciate this quick response. I am usually on reddit for help. I am playing with my code. It seems i need to put string to int conversion in before the IF statements, and I need to either append my list, or push its range out to 15. I have a constant LIMIT set to 15, i thought that would establish the size. Im working with C++ as well, so i get a little confused. – Ian Schaak May 04 '22 at 19:10
  • You can wriite `int(input(''))` to convert at the moment, but if you do that, make sure to do a try/except block with `ValueError` exception in case the user doesn't enter a number. By other hand, you still can use `LIMIT` to establish the size, just check the question (the link) in the answer. – Jony_23 May 04 '22 at 19:18
  • You're welcome. Could you consider accepting my answer, please? [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Jony_23 May 04 '22 at 22:11