Displaying Personal Information in Python with 30 Lines of Code

Python is a popular and versatile programming language that allows us to quickly create scripts for various tasks. In this blog post, we’ll explore how to use Python to output personal information within 30 lines of code.

First, let’s define the personal information we want to display. For simplicity, we’ll consider the following fields: name, age, occupation, and email.

Here’s an example of a 30-line Python program that outputs personal information:

python# Define personal information
name = "John Doe"
age = 30
occupation = "Software Engineer"
email = "johndoe@example.com"

# Function to display personal information
def display_info():
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Occupation: {occupation}")
print(f"Email: {email}")

# Add a function to format the information into a neatly formatted block
def format_info():
info_block = f"""
Name: {name}
Age: {age}
Occupation: {occupation}
Email: {email}
"""

return info_block

# Call the function to display the information
print(format_info())

# Optionally, you can also call the display_info() function for a simpler output
# display_info()

# Additional functionality: Prompting user for input (optional)
# def get_user_info():
# global name, age, occupation, email
# name = input("Enter your name: ")
# age = int(input("Enter your age: "))
# occupation = input("Enter your occupation: ")
# email = input("Enter your email: ")
# print(format_info())

# get_user_info() # Uncomment this line if you want to prompt the user for input

In this program, we first define the personal information as variables. Then, we create two functions: display_info() to print the information line by line, and format_info() to format the information into a neatly formatted block. We choose to use the format_info() function and print its return value to display the information in a clean and structured way.

Additionally, I’ve included an optional section (get_user_info()) that prompts the user for input and updates the personal information variables accordingly. You can uncomment the relevant lines if you want to incorporate this functionality into your program.

Remember, the goal of this program is to demonstrate how to output personal information within 30 lines of code. Depending on your specific requirements, you may need to adjust the program accordingly.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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