The sport of amateur boxing requires opponents to be in the same weight category to compete. For this exercise, we will consider the following categories for weights of women and men:
Class Name | Weight in kg |
Welterweight | 61-64 |
Lightweight | 58-60 |
Featherweight | 55-57 |
Bantamweight | 51-54 |
Write a Python program that prompts for the weights of boxing competitors. Weights for this competition are between 51 and 64 kg. Accept the weights of all competitors until a zero or a negative number is entered. When all entries have been entered, determine how many competitors are in each category. If there are zero competitors in a category, do not list it. If there is only one competitor in a category, add one to the next higher category if there is one. If there is a single welterweight, do not move this competitor from the category. If a competitor weight is below 51 or above 64, add them to one of two new categories: TooLow, TooHigh. Calculate and print the average weight of all competitors in the competition. Your output must be self-explanatory.
Include a flowchart with your program.
Use the following data to check your work: 51, 59, 62, 63, 49, 54, 53, 52, 55, 66, 55, 50, 57, 0
Note: The code is for Python 3. Please indent the code as shown in the screenshots.
Programs screenshots:
Sample output:
Code to copy:
# Define the main() function.
def main():
# Create a dictionary to store the categories.
className = {"Welterweight": 0, "Lightweight": 0, "Featherweight": 0, "Bantamweight": 0, "TooLow": 0, "TooHigh": 0}
# Declare the required variables.
competitors = 0
total = 0
# Prompt the user to enter the weights.
print("Enter the weights terminated by 0 or a negative value:")
# Start the while loop.
while(True):
# Store the weight entered by the user.
weight = int(input())
# Break the loop if the input is 0 or negative.
if weight <= 0:
break
# Increment the total and number of competitors
# if the weight is valid.
if weight <=64 and weight >= 51:
total += weight
competitors += 1
# Increase the count of too high if the weight
# is greater than 64.
if weight > 64:
className["TooHigh"] += 1
# Increase the count of too low if the weight
# is less than 51.
elif weight < 51:
className["TooLow"] += 1
# Increase the count of Bantamweight if the weight
# is greater than 51 and less than 54.
elif weight >=51 and weight <= 54:
className["Bantamweight"] += 1
# Increase the count of Featherweight if the weight
# is greater than 55 and less than 57.
elif weight >=55 and weight <= 57:
className["Featherweight"] += 1
# Increase the count of Lightweight if the weight
# is greater than 58 and less than 60.
elif weight >=58 and weight <= 60:
className["Lightweight"] += 1
# Increase the count of Welterweight if the weight
# is greater than 60 and less than 64.
elif weight >=60 and weight <= 64:
className["Welterweight"] += 1
# Increase the count of Featherweight if the there
# is only 1 entry in Bantamweight.
if className["Bantamweight"] == 1:
className["Featherweight"] += 1
className["Bantamweight"] = 0
# Increase the count of Lightweight if the there
# is only 1 entry in Featherweight.
if className["Featherweight"] == 1:
className["Lightweight"] += 1
className["Featherweight"] = 0
# Increase the count of Welterweight if the there
# is only 1 entry in Lightweight.
if className["Lightweight"] == 1:
className["Welterweight"] += 1
className["Lightweight"] = 0
# Compute the average weight of all the competitors.
average = total/competitors
print("The number of competitors in each category is as follows:")
# Start the loop to traverse the dictionary.
for category in className.keys():
# Print the category and its count if the count is
# not equal to 0.
if className[category] != 0:
print(category + ":", className[category])
# Display the average weight.
print("The average weight of all competitors is", average)
# Call the main() function.
if __name__ == "__main__":
main()
Get Answers For Free
Most questions answered within 1 hours.