Python

Python programme

To create a Python program for article posting, we need to define the required functionalities such as inputting the article content, publishing it, and displaying it to the user. Here’s an example program that demonstrates these functionalities:

# Initialize empty list to store articles
articles = []

# Define function to create new article
def create_article():
    # Input article details from user
    title = input("Enter article title: ")
    content = input("Enter article content: ")
    author = input("Enter author name: ")
    
    # Create dictionary to store article details
    article = {
        "title": title,
        "content": content,
        "author": author
    }
    
    # Add article to articles list
    articles.append(article)
    print("Article created successfully!")

# Define function to display all articles
def display_articles():
    if not articles:
        print("No articles found!")
        return
    
    for i, article in enumerate(articles):
        print(f"{i+1}. {article['title']} by {article['author']}")
        print(article['content'])
        print()

# Define main function
def main():
    while True:
        print("1. Create new article")
        print("2. Display all articles")
        print("3. Quit")
        
        choice = input("Enter your choice: ")
        
        if choice == "1":
            create_article()
        elif choice == "2":
            display_articles()
        elif choice == "3":
            print("Exiting program.")
            break
        else:
            print("Invalid choice. Please try again.")

# Call main function
if __name__ == "__main__":
    main()

In this program, we start by initializing an empty list to store the articles. We define two functions, create_article() and display_articles(), to create new articles and display all existing articles respectively. The create_article() function prompts the user to input the title, content, and author of the article and stores these details in a dictionary. The dictionary is then added to the articles list. The display_articles() function simply iterates over the articles list and displays the title, author, and content of each article. Finally, we define the main() function to provide a menu of options for the user to choose from. The program runs until the user chooses to quit by entering option 3.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button