Exercise
Advanced Statistics
Objective
Develop a Python statistical program which will allow the user to:
- Add new data
- See all data entered
- Find an item, to see whether it has been entered or not
- View a summary of statistics: amount of data, sum, average, maximum, minimum
- Exit the program
These options must appear as a menu. Each option will be chosen by a number or a letter.
The program must reserve space for a maximum of 1000 data, but keep count of how many data actually exist.
Example Python Exercise
Show Python Code
# Program developed by: Programmer 1, Programmer 2
# Initialize list to store data, with a maximum size of 1000
data = []
max_data_size = 1000
# Main program loop
while True:
print("\nMenu:")
print("1. Add new data")
print("2. See all data entered")
print("3. Find an item")
print("4. View summary of statistics")
print("5. Exit")
choice = input("Choose an option (1-5): ").strip().lower()
if choice == "1" or choice == "a":
if len(data) < max_data_size:
try:
new_data = float(input("Enter a number to add to the data: "))
data.append(new_data)
print("Data added successfully!")
except ValueError:
print("Invalid input. Please enter a valid number.")
else:
print("Maximum data limit reached. Cannot add more data.")
elif choice == "2" or choice == "b":
if data:
print("Data entered so far:")
for item in data:
print(item)
else:
print("No data entered yet.")
elif choice == "3" or choice == "c":
try:
item_to_find = float(input("Enter the number to find: "))
if item_to_find in data:
print(f"Item {item_to_find} found in the data.")
else:
print(f"Item {item_to_find} not found in the data.")
except ValueError:
print("Invalid input. Please enter a valid number.")
elif choice == "4" or choice == "d":
if data:
total_data = len(data)
data_sum = sum(data)
average = data_sum / total_data
maximum = max(data)
minimum = min(data)
print(f"Data Summary:")
print(f"Amount of data: {total_data}")
print(f"Sum of data: {data_sum}")
print(f"Average: {average:.2f}")
print(f"Maximum: {maximum}")
print(f"Minimum: {minimum}")
else:
print("No data entered yet. Cannot display statistics.")
elif choice == "5" or choice == "e":
print("Exiting the program.")
break
else:
print("Invalid choice, please select a valid option.")
Output
Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 1
Enter a number to add to the data: 25
Data added successfully!
Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 1
Enter a number to add to the data: 30
Data added successfully!
Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 2
Data entered so far:
25.0
30.0
Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 3
Enter the number to find: 25
Item 25.0 found in the data.
Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 4
Data Summary:
Amount of data: 2
Sum of data: 55.0
Average: 27.50
Maximum: 30.0
Minimum: 25.0
Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 5
Exiting the program.
Share this Python Exercise