181k views
1 vote
My average is not coming out right in the python program. It should be .2365 yet it's coming back as 0.061125. I'm not sure what I've done wrong or how it's coming up with that number. Any help would be appreciated! Here is my assignment and my code.

In this lab, you complete a partially prewritten Python program that uses a list.

The program prompts the user to interactively enter eight batting averages, which the program stores in an array. It should then find the minimum and maximum batting averages stored in the array, as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help you write the remainder of the program.

Instructions
Make sure the file BattingAverage.py is selected and open.
Write the Python statements as indicated by the comments.
Execute the program by clicking the Run button at the bottom of the screen. Enter the following batting averages: .299, .157, .242, .203, .198, .333, .270, .190. The minimum batting average should be .157 and the maximum batting average should be .333. The average should be .2365.

# Declare a named constant for array size here.
MAX_AVERAGES = 8

# Declare array here.
averages = []
# Write a loop to get batting averages from user and assign to array.
for i in range(MAX_AVERAGES):
averageString = input("Enter a batting average: ")
battingAverage = float(averageString)
# Assign value to array.
averages.append(battingAverage)
# Assign the first element in the array to be the minimum and the maximum.
minAverage = averages[0]
maxAverage = averages[0]
# Start out your total with the value of the first element in the array.
total = averages[0]
# Write a loop here to access array values starting with averages[1]
for i in range(1, MAX_AVERAGES):
if averages[i] < minAverage:
minAverage = averages[i]
if averages[i] > maxAverage:
maxAverage = averages[i]
# Within the loop test for minimum and maximum batting averages.
total += averages[i]


# Also accumulate a total of all batting averages.
average = total / MAX_AVERAGES

# Calculate the average of the 8 batting averages.


# Print the batting averages stored in the averages array.
for avg in averages:
print(avg)

# Print the maximum batting average, minimum batting average, and average batting average.
print('Minimum batting average is ' + str(minAverage))
print('Maximum batting average is ' + str(maxAverage))
print('Average batting average is ' + str(average))

1 Answer

6 votes

Final answer:

The provided Python program has a logic error in accumulating the total for the batting averages; it incorrectly starts with the first element before the loop and does not include that in the loop's iteration. The correct method is to start the total at zero and add all elements of the averages list within the loop to calculate the average correctly.

Step-by-step explanation:

The issue with the Python program provided for calculating the batting average is located in the loop where the total of batting averages is accumulated. The problem specifically lies in the fact that you are only starting your total with the first element and then adding to this total inside the loop, which should include the first element from the start. The loop for calculating the total should not have started adding from averages[1] but should have started from averages[0] instead. Moreover, the increment of the total variable should be within the for-loop.

Here is the corrected code for the loop that calculates the total and averages:

total = 0
for batting_average in averages:
total += batting_average
average = total / MAX_AVERAGES

Once you update your code with the correct total calculation logic, the program should provide the correct average batting average of .2365.

User Luke Puplett
by
8.1k points