Software Applications - Python Programming Exercise

In this exercise, you will develop a Python program that can store up to 1,000 records of software applications. For each application, you must keep the following data: Name, Category, Description, and Version (a set of 3 data points: version number -text-, launch month -byte-, and launch year -unsigned short-). The program should allow the user the following operations: add data of a new application (the name must not be empty, category must not exceed 30 letters (or should be re-entered), and for the description, it will accept only the first 100 letters entered and skip the rest; the version needs no validation), show the names of all the stored applications (each name must appear on one line; if there are more than 20 applications, you must pause after displaying each block of 20 applications, and wait for the user to press Enter before proceeding; the user should be notified if there is no data), view all data for a certain application (from part of its name, category, or description, case sensitive; if there are several applications that contain that text, the program will display all of them, separated by a blank line; the user should be notified if there are no matches found), update a record (asking the user for the number, the program will display the previous value of each field, and the user can press Enter not to modify any of the data; they should be warned (but not asked again) if they enter an incorrect record number; it is not necessary to validate any of the fields), delete a record (whose number will be indicated by the user; they should be warned (but not asked again) if they enter an incorrect number), sort the data alphabetically by name, fix redundant spaces (turn all sequences of two or more spaces into a single space, only in the name, for all existing records), and exit the application (as we do not store the information, data will be lost). This exercise is perfect for practicing the use of arrays and data manipulation in Python, as well as enhancing your skills in user interaction and database management. By implementing these functionalities, you will gain hands-on experience in handling user input and output in Python. This exercise not only reinforces your understanding of arrays and data manipulation but also helps you develop efficient coding practices for managing large datasets.

 Category

Arrays, Lists, and Strings

 Exercise

Software Applications

 Objective

Develop a Python program that can store up to 1000 records of software applications. For each application, you must keep the following data:

* Name
* Category
* Description
* Version (a set of 3 data points: version number -text-, launch month -byte-, and launch year -unsigned short-)

The program should allow the user the following operations:
1 - Add data of a new application (the name must not be empty, category must not exceed 30 letters (or should be re-entered), and for the description, it will accept only the first 100 letters entered and skip the rest; the version needs no validation).
2 - Show the names of all the stored applications. Each name must appear on one line. If there are more than 20 applications, you must pause after displaying each block of 20 applications, and wait for the user to press Enter before proceeding. The user should be notified if there is no data.
3 - View all data for a certain application (from part of its name, category, or description, case sensitive). If there are several applications that contain that text, the program will display all of them, separated by a blank line. The user should be notified if there are no matches found.
4 - Update a record (asking the user for the number, the program will display the previous value of each field, and the user can press Enter not to modify any of the data). They should be warned (but not asked again) if they enter an incorrect record number. It is not necessary to validate any of the fields.
5 - Delete a record, whose number will be indicated by the user. They should be warned (but not asked again) if they enter an incorrect number.
6 - Sort the data alphabetically by name.
7 - Fix redundant spaces (turn all sequences of two or more spaces into a single space, only in the name, for all existing records).
X - Exit the application (as we do not store the information, data will be lost).

 Example Python Exercise

 Copy Python Code
# Class to represent a software application
class SoftwareApplication:
    # Constructor to initialize the application with name, category, description, and version
    def __init__(self, name, category, description, version):
        self.name = name  # The name of the application
        self.category = category  # The category of the application
        self.description = description  # The description of the application
        self.version = version  # Version as a tuple (version number, month, year)

    # String representation of the application for easy display
    def __str__(self):
        # Format the application details as a string for display
        return f"Name: {self.name}\nCategory: {self.category}\nDescription: {self.description}\nVersion: {self.version[0]} (Month: {self.version[1]}, Year: {self.version[2]})"


# List to store all the applications entered by the user
applications = []

# Start the main program loop
while True:
    # Display the main menu to the user
    print("\nMenu:")
    print("1 - Add data of a new application")
    print("2 - Show all stored applications")
    print("3 - View all data for a certain application")
    print("4 - Update a record")
    print("5 - Delete a record")
    print("6 - Sort data alphabetically by name")
    print("7 - Fix redundant spaces in application names")
    print("X - Exit")
    
    # Get the user's choice from the menu
    choice = input("Enter your choice: ").strip().lower()

    # Option 1: Add a new application
    if choice == '1':
        # Get user input for the application name, ensuring it's not empty
        name = input("Enter application name: ").strip()
        if not name:  # Check if the name is empty
            print("Name cannot be empty.")  # Display an error message
            continue  # Skip to the next iteration of the loop

        # Get the category, ensuring its length is within the limit of 30 characters
        category = input("Enter category (max 30 characters): ").strip()
        while len(category) > 30:  # Check if the category exceeds 30 characters
            print("Category is too long. Please re-enter.")  # Error message
            category = input("Enter category (max 30 characters): ").strip()  # Prompt again

        # Get the description and truncate it to the first 100 characters
        description = input("Enter description (max 100 characters): ").strip()
        description = description[:100]  # Truncate the description to 100 characters

        # Get the version number, launch month, and launch year
        version_number = input("Enter version number: ").strip()
        launch_month = int(input("Enter launch month (1-12): ").strip())
        launch_year = int(input("Enter launch year: ").strip())

        # Create a new SoftwareApplication object and add it to the list
        applications.append(SoftwareApplication(name, category, description, (version_number, launch_month, launch_year)))

    # Option 2: Show all stored applications
    elif choice == '2':
        if not applications:  # Check if there are no applications stored
            print("No applications stored.")  # Display a message if no applications exist
        else:
            counter = 1  # Initialize a counter to display application numbers
            for app in applications:  # Iterate through all stored applications
                print(f"\n{counter}. {app}")  # Display the application
                counter += 1  # Increment the counter
                if counter % 20 == 0:  # Check if 20 applications have been displayed
                    input("\nPress Enter to continue...")  # Pause and wait for user input

    # Option 3: View all data for a certain application
    elif choice == '3':
        # Prompt the user for search text
        search_text = input("Enter part of the name, category, or description to search: ").strip()
        found = False  # Flag to indicate if any applications were found
        for app in applications:  # Iterate through all stored applications
            # Check if the search text is found in the name, category, or description (case-insensitive)
            if (search_text.lower() in app.name.lower() or 
                search_text.lower() in app.category.lower() or 
                search_text.lower() in app.description.lower()):
                print(f"\n{app}")  # Display the application details if a match is found
                found = True  # Set the flag to True if a match is found
        if not found:  # If no matches were found
            print("No matches found.")  # Inform the user

    # Option 4: Update an existing record
    elif choice == '4':
        try:
            # Prompt the user for the record number to update
            record_number = int(input("Enter the record number to update: ").strip()) - 1
            if record_number < 0 or record_number >= len(applications):  # Validate the record number
                print("Invalid record number.")  # Display an error message
                continue  # Skip to the next iteration of the loop

            app = applications[record_number]  # Get the application to update
            # Display the current data of the application
            print(f"\nCurrent data for application {record_number + 1}:")
            print(f"Name: {app.name}\nCategory: {app.category}\nDescription: {app.description}\nVersion: {app.version[0]} (Month: {app.version[1]}, Year: {app.version[2]})")

            # Prompt the user for new values, or allow them to press Enter to keep the current ones
            new_name = input(f"Enter new name (current: {app.name}): ").strip()
            if new_name:  # If the user entered a new name
                app.name = new_name  # Update the name

            new_category = input(f"Enter new category (current: {app.category}): ").strip()
            if new_category:  # If the user entered a new category
                app.category = new_category  # Update the category

            new_description = input(f"Enter new description (current: {app.description}): ").strip()
            if new_description:  # If the user entered a new description
                app.description = new_description[:100]  # Update the description (truncate to 100 characters)

            new_version_number = input(f"Enter new version number (current: {app.version[0]}): ").strip()
            if new_version_number:  # If the user entered a new version number
                app.version = (new_version_number, app.version[1], app.version[2])  # Update the version number

            new_month = input(f"Enter new launch month (current: {app.version[1]}): ").strip()
            if new_month:  # If the user entered a new launch month
                app.version = (app.version[0], int(new_month), app.version[2])  # Update the launch month

            new_year = input(f"Enter new launch year (current: {app.version[2]}): ").strip()
            if new_year:  # If the user entered a new launch year
                app.version = (app.version[0], app.version[1], int(new_year))  # Update the launch year

        except ValueError:  # If the input is not a valid integer
            print("Please enter a valid number.")  # Display an error message

    # Option 5: Delete a record
    elif choice == '5':
        try:
            # Prompt the user for the record number to delete
            record_number = int(input("Enter the record number to delete: ").strip()) - 1
            if record_number < 0 or record_number >= len(applications):  # Validate the record number
                print("Invalid record number.")  # Display an error message
                continue  # Skip to the next iteration of the loop

            # Delete the application from the list
            del applications[record_number]
            print(f"Application {record_number + 1} deleted.")  # Inform the user

        except ValueError:  # If the input is not a valid integer
            print("Please enter a valid number.")  # Display an error message

    # Option 6: Sort data alphabetically by name
    elif choice == '6':
        applications.sort(key=lambda app: app.name.lower())  # Sort the applications by name (case-insensitive)
        print("Applications sorted alphabetically by name.")  # Inform the user

    # Option 7: Fix redundant spaces in application names
    elif choice == '7':
        for app in applications:  # Iterate through all applications
            app.name = ' '.join(app.name.split())  # Replace multiple spaces with a single space
        print("Redundant spaces fixed in application names.")  # Inform the user

    # Option X: Exit the program
    elif choice == 'x':
        print("Exiting the program...")  # Inform the user
        break  # Exit the loop and end the program

    else:
        print("Invalid choice, please enter a valid option.")  # Display an error message if the choice is invalid

 Output

Menu:
1 - Add data of a new application
2 - Show all stored applications
3 - View all data for a certain application
4 - Update a record
5 - Delete a record
6 - Sort data alphabetically by name
7 - Fix redundant spaces

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Exercise tasks

    In this exercise, you will develop a Python program that can store up to 2,000 "to-do tasks". For each task, it must keep the following data: Date (a set of 3 ...

  •  Home Finances

    In this exercise, you will develop a Python program that can store up to 10,000 records of expenses and incomes to create a small household accounting system. For eac...

  •  Array Reversal

    This Python program prompts the user to input 5 numbers and stores them in an array (or list in Python). After all the numbers are entered, the program displays them ...

  •  Array Lookup

    his Python program asks the user to input a specific number of floating-point numbers and stores them in a list. The program then repeatedly prompts the user to enter...

  •  Even Number Array

    This Python program prompts the user to enter 10 integer numbers and displays only the even numbers from the list. It utilizes a for loop to iterate through th...

  •  Positive and Negative Number Array

    This Python program prompts the user to enter 10 real numbers and calculates two averages: one for the positive numbers and another for the negative numbers...