Class Project: Name and Age Python Program


For my Introduction to Scripting class, I created a Python program named NameAge.py. This project was designed to apply my programming skills by developing a simple yet functional program that interacts with users, processes input, and performs calculations to provide meaningful output.


# Author: Silvia Pizarro McCants
# Date: May 17, 2024
# Purpose: This program prompts the user for their name, age, birth month, and birthdate,
# then calculates and displays the year they were born in.

# Get the current year and month
current_year = datetime.now().year
current_month = datetime.now().month

# Prompt the user to enter their name
name = input("What is your name? ")

# Prompt the user to enter their age
age = int(input("How old are you? "))

# Prompt the user to enter their birth month
birth_month = int(input("What is your birth month (1-12)? "))

# Prompt the user to enter their birthdate
birth_date = int(input("What is your birth date (1-31)? "))

# Calculate the birth year considering the current month and birth month
# Explanation:
# I updated the code to prompt the user for their birth month and date to calculate
# the birth year more accurately. By knowing the birth month and date, the program
# can determine if the user has already had their birthday this year. This refinement
# ensures the birth year calculation is precise, providing more reliable and meaningful
# results, especially for users who have not yet had their birthday this year.
if birth_month > current_month:
    birth_year = current_year - age - 1
else:
    birth_year = current_year - age

# Print the greeting message
print(f"Hello {name}! You were born on {birth_month}/{birth_date}/{birth_year}.")

Project Overview

Objective: The main objective of this project was to create a program that prompts users for their personal information, including their name, age, birth month, and birthdate. The program then calculates the year they were born and displays this information in a friendly message.

IDE Used: For this project, I used PyCharm, an integrated development environment (IDE) specifically designed for Python programming. PyCharm offers robust code editing features, debugging tools, and a user-friendly interface, which significantly enhanced my coding experience and productivity.


Key Features

User Interaction:

  • Prompts for User Input: The program asks users to enter their name, age, birth month, and birthdate.
  • Greeting Message: After processing the input, the program greets the user and provides the calculated birth year.

Calculations:

  • Current Date: Utilizes Python’s datetime module to get the current year and month.
  • Birth Year Calculation: Calculates the birth year based on the current date and the user’s provided age, birth month, and birthdate. The calculation takes into account whether the user’s birthday has occurred yet this year to ensure accuracy.

    Code Breakdown

    • Importing the datetime Module:
      • from datetime import datetime
      • This module is used to get the current date and time, which is essential for calculating the birth year.
    • Getting Current Year and Month:
      • current_year = datetime.now().year
      • current_month = datetime.now().month
    • User Input:
      • The program prompts the user to enter their name, age, birth month, and birthdate using the input() function.
    • Birth Year Calculation:
      • The program uses the provided birth month and the current month to determine if the user’s birthday has already occurred this year. Based on this information, it adjusts the birth year calculation accordingly.
    • Output:
      • The program prints a message that includes the user’s name and calculated birth year.

    Example Output

    What is your name? John
    How old are you? 25
    What is your birth month (1-12)? 7
    What is your birth date (1-31)? 15
    Hello John! You were born on 7/15/1998.


    Learning Outcomes

    This project allowed me to:

    • Practice User Input Handling: Enhanced my skills in collecting and processing user input in Python.
    • Perform Date Calculations: Applied date and time functions to perform accurate calculations based on user input.
    • Enhance Code Readability: Followed best practices for code readability, including clear variable naming and comments explaining each step.
    • Debugging and Testing: Gained experience in debugging and testing to ensure the program works correctly under various scenarios.

    Overall, the NameAge.py program is a straightforward yet effective application of fundamental programming concepts, showcasing my ability to create interactive and functional code using PyCharm as my development environment.


    Silvia Pizarro McCants | UX & Graphic Design Portfolio

    Unsplash. (n.d.). Photo by Roman Synkevych on unsplash. Unsplash.com. Retrieved June 7, 2024, from https://unsplash.com/photos/black-android-smartphone-vXInUOv1n84

    Print Friendly, PDF & Email