from collections import namedtuple
# Define the NamedTuple for a 2D point with color data
Point = namedtuple('Point', ['x', 'y', 'r', 'g', 'b'])
# Create an array to store up to 1000 points
points = []
# Display the menu
while True:
print("\nMenu:")
print("1. Add data for one point")
print("2. Display all entered points")
print("3. Calculate and display the average values for x and y")
print("4. Exit")
# Ask the user to choose an option
option = input("Choose an option (1, 2, 3, 4): ")
if option == "1":
# Add data for one point
print("Enter the data for a new point:")
x = int(input("Enter x coordinate (short): "))
y = int(input("Enter y coordinate (short): "))
r = int(input("Enter red color value (byte): "))
g = int(input("Enter green color value (byte): "))
b = int(input("Enter blue color value (byte): "))
# Store the new point in the array
points.append(Point(x, y, r, g, b))
print("Point added successfully.")
elif option == "2":
# Display all entered points
if points:
print("\nAll entered points:")
for i, point in enumerate(points, 1):
print(f"Point {i}: x: {point.x}, y: {point.y}, r: {point.r}, g: {point.g}, b: {point.b}")
else:
print("No points entered yet.")
elif option == "3":
# Calculate and display the average values for x and y
if points:
avg_x = sum(point.x for point in points) / len(points)
avg_y = sum(point.y for point in points) / len(points)
print(f"\nAverage x: {avg_x:.2f}")
print(f"Average y: {avg_y:.2f}")
else:
print("No points entered yet to calculate averages.")
elif option == "4":
# Exit the program
print("Exiting the program.")
break
else:
# Invalid option
print("Invalid option. Please choose a valid number (1, 2, 3, 4).")
Output
Menu:
1. Add data for one point
2. Display all entered points
3. Calculate and display the average values for x and y
4. Exit
Choose an option (1, 2, 3, 4): 1
Enter the data for a new point:
Enter x coordinate (short): 5
Enter y coordinate (short): 10
Enter red color value (byte): 255
Enter green color value (byte): 100
Enter blue color value (byte): 50
Point added successfully.
Menu:
1. Add data for one point
2. Display all entered points
3. Calculate and display the average values for x and y
4. Exit
Choose an option (1, 2, 3, 4): 1
Enter the data for a new point:
Enter x coordinate (short): 20
Enter y coordinate (short): 25
Enter red color value (byte): 0
Enter green color value (byte): 255
Enter blue color value (byte): 200
Point added successfully.
Menu:
1. Add data for one point
2. Display all entered points
3. Calculate and display the average values for x and y
4. Exit
Choose an option (1, 2, 3, 4): 2
All entered points:
Point 1: x: 5, y: 10, r: 255, g: 100, b: 50
Point 2: x: 20, y: 25, r: 0, g: 255, b: 200
Menu:
1. Add data for one point
2. Display all entered points
3. Calculate and display the average values for x and y
4. Exit
Choos