Main Function Output Value - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user to provide a title via the command line (using the previously defined WriteTitle function). If no input is given, the program should display an error message and return an exit code of 1 to the operating system. This exercise is perfect for practicing function definition, command-line input handling, and error handling in Python. By implementing this program, you will gain hands-on experience in handling function definitions, command-line input, and error handling in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Main Function Output Value

 Objective

Develop a Python program that prompts the user to provide a title via the command line (using the previously defined WriteTitle function). If no input is given, the program should display an error message and return an exit code of 1 to the operating system.

 Example Python Exercise

 Copy Python Code
import sys  # Import the sys module to handle system exit codes

# Function to display a title in uppercase, centered with extra spaces and lines above and below it
def WriteTitle(text):
    # Convert the text to uppercase
    text = text.upper()
    
    # Calculate the total width of the screen (80 characters)
    screen_width = 80
    
    # Calculate how many spaces are needed on the left and right to center the text
    spaces = (screen_width - len(text) - 2) // 2  # The extra 2 accounts for the space on both sides of the text
    
    # Create a line of hyphens based on the text length
    line = '-' * (len(text) + 2)  # The 2 extra characters account for spaces around the text
    
    # Print a line of hyphens above the text
    print(line)
    
    # Print the text centered with spaces on both sides
    print(' ' * spaces + text + ' ' * spaces)
    
    # Print a line of hyphens below the text
    print(line)

# Main function to prompt the user for a title and display it
def main():
    # Prompt the user for a title
    title = input("Please enter a title: ").strip()  # .strip() is used to remove leading and trailing spaces
    
    # Check if the title is empty
    if not title:
        # Print an error message if no title is provided
        print("Error: No title provided.")
        
        # Exit the program with an exit code of 1 to indicate an error
        sys.exit(1)
    
    # Call the WriteTitle function to display the title if valid input is provided
    WriteTitle(title)

# Run the main function if the script is executed directly
if __name__ == "__main__":
    main()

 Output

Please enter a title: Hello World

--------------- HELLO WORLD ---------------

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

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.

  •  Function to Calculate DV

    In this exercise, you will develop a Python program that defines a function to count the number of numeric digits and vowels in a given string. The function should ta...

  •  Function to Verify Alphabetic Characters

    In this exercise, you will develop a Python program that includes a function to determine if a given character is alphabetic (from A to Z). This exercise is pe...

  •  Function to Check Numeric Value

    In this exercise, you will develop a Python program that defines a function to check if a string represents an integer value. This exercise is perfect for prac...

  •  Main Function Parameters for Calculator

    In this exercise, you will develop a Python program that performs basic arithmetic operations such as addition, subtraction, multiplication, or division based on comm...

  •  Main Function Parameters and Return Value for Calculator

    In this exercise, you will develop a Python program that performs arithmetic operations such as addition, subtraction, multiplication, or division by analyzing comman...

  •  Function to Find Array's Min and Max Values

    In this exercise, you will develop a Python program that defines a function to find the smallest and largest numbers in a list. The function should accept the list as...