how do fix IndexError: list index out of range while checking if an input is an integer?

Question:

i’m (a beginner) trying to create a python program that adds up the points of a team for the game capturing olympus, this is my whole program

# initialise variables
player_names = []
player_scores = []
total = 0
points = 0
top_score = 0
score_valid = False

# main program

# opening message
print("welcome to capturing olympus point counter! begin by entering your team name.n")

# get team name
team_name = input("nteam name: ")

# prompt to enter player names
print("nokay " + team_name + ", enter each of your 6 players names.n")

# loop to create list of players names
for x in range(6):
    name = input("enter the name of player " + str(x + 1) + ": ")
    player_names.append(name)

# loop for players to input their scores
for x in range(6):
    while score_valid == False:
        score_input = input("n" + str(player_names[x]) + ", enter the number of hits you scored: ")
        if type(int(score_input)) == int:
            score_valid = True
            score = int(score_input)
            player_scores.append(score)
        else:
            print("try again.")
            score_valid = False

# calculate total loop
for x in range(6):
    total += player_scores[x]

# find average
average = round(float(total / 6), 2)

# find points
if total > 50:
    points += 1
if average >= 10.00:
    points += 1

# blank space
print("")

# results
if points == 1:
    print("well done " + team_name + ", you earned a point!")
elif points == 2:
    print("WOW! " + team_name + ", you earned 2 points!")
elif points == 0:
    print("you earned 0 points. better luck next time!")

# find top score
for each_score in player_scores:
    if each_score > top_score:
        top_score = each_score
# find top scorer
i = player_scores.index(top_score)
top_scorer = player_names[i]

print("your top scorer was " + top_scorer + ", with " + str(top_score) + " points!")

in the section where the user inputs the scores, i’m trying to create a loop so that if the user doesn’t enter an integer, it prompts them again.

# loop for players to input their scores
for x in range(6):
    while score_valid == False:
        score_input = input("n" + str(player_names[x]) + ", enter the number of hits you scored: ")
        if type(int(score_input)) == int:
            score_valid = True
            score = int(score_input)
            player_scores.append(score)
        else:
            print("try again.")
            score_valid = False

i tried a lot of different ways to test if the input is an integer, this is the only one where the code has been able to run past these lines, but then it gets to this section:

# calculate total loop
for x in range(6):
    total += player_scores[x]

and gives me the error:

IndexError: list index out of range

i don’t get any errors if i remove the section where i check i don’t know how to fix this, thank you for any help.

Asked By: crashlanding

||

Answers:

You build player_scores through the statement player_scores.append(score). So, what is the size of player_scores after your loops? I think len(player_scores) is not always == 6, because you are controlling the loop through while score_valid == False:. The variable score_valid is not reset to False for the next player after entering data for one player successfully, but leaves it with True.

You may need to change from

# loop for players to input their scores
for x in range(6):
    while score_valid == False:
        ...

to

# loop for players to input their scores
for x in range(6):
    score_valid = False
    while score_valid == False:
        ....
Answered By: MatthiasL
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.