我可以输入一次。在输入第二个lastNames输入后,它给了我索引错误。它为什么这样做?这个的Index应该是1。范围是15,为什么会这样?我怎么修理它?
代码如下:
#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)
lastNames = enrollees = [15]
你的赋值不正确,只有一个大小为1的列表。我想这就是你出错的原因。你必须创建一个大小为15的列表。
这个问题可以帮助你:在Python中创建一个具有一定大小的空列表