Trying to create a win/loss/tie list based on team scores. My attempt:
#import numpy
import numpy as np
#create array for week numbers
week_number = np.array(['Week 1','Week 2','Week 3','Week 4','Week 5','Week 6'])
#create arrays for scores
my_team_score = np.array([41,17,28,21,10,18])
opposing_team_score = np.array([33,11,30,28,17,30])
#create iteration to check win, tie, or loss and print result
for i in my_team_score:
if my_team_score[i] > opposing_team_score[i]:
print(week_number[i] ': Win')
elif my_team_score[i] == opposing_team_score[i]:
print(week_number[i] ': Tie')
else:
print(week_number[i] ': Loss')
Receiving error:
IndexError: index 41 is out of bounds for axis 0 with size 6
CodePudding user response:
When iterating over an iterable, what you have is a value itself, not an indice. Here i will value 41, 17, 28, 21, 10, 18, at each iteration.
To get an indice you'd use for i in range(len(my_team_score)):
But use zip to get a nicer code
for a_score, b_score, week in zip(my_team_score, opposing_team_score, week_number):
if a_score > b_score:
print(week ': Win')
elif a_score == b_score:
print(week ': Tie')
else:
print(week ': Loss')
