Click here to Skip to main content
15,881,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that has a text file and what I am trying to do is grab the data and print it. However, when I look at my batting avergae, it is only grabbing the first average not the highest batting average
PERL
ef getLists():

    # Create a Variable to ask the user to get the file
    fname = input("Enter the name of the MLB stats data file: ")
    infile = open(fname, 'r')
    # Creates lists for the text file
    players = []
    teams = []
    positions = []
    runs = []
    hits = []
    homeruns = []
    averages = []

    line = infile.readline()
    for line in infile:
        line = line.strip()
        player, team, position, run, hit, homerun, average = line.split(',')
        players = players + [player]
        teams = teams + [team]
        positions = positions + [position]
        runs = runs + [run]
        hits = hits + [int(hit)]
        homeruns = homeruns + [int(homerun)]
        averages = averages + [float(average)]
    return players, teams, positions, runs, hits, homeruns, averages



# Find's the player with the highest average
def getHighestAverage(players, averages):
    highestAverage = 0
    for i in range(len(averages)):
        if highestAverage >= averages[i]:
            highestAverage = [i]

    print(players[highestAverage],averages[highestAverage])
Posted
Updated 10-Dec-13 9:39am
v2
Comments
YvesDaoust 10-Dec-13 10:28am    
BTW: users of this forum don't like minimalistic explanations together with lengthy code, where the reader has to do all the work

1 solution

You are confusing the value and the index. Use
C++
if highestAverage >= averages[i]:
    highestIndex= i
    highestAverage= averages[i]

print(players[highestIndex], averages[highestIndex])
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900