Scrollable Nav Bar

Python Program to Take Users Name and Age as Input and Print a Greeting Message

In this program, we take the user’s name and age as input and display a personalized greeting message. The goal is to understand how Python accepts user input from the keyboard and how we can use that input to produce formatted output. For example, if the user enters the name Alice and age 23, the program will display a message such as: Hello, Alice! You are 23 years old.

Example

Input:

Name: Alice
Age: 23

Expected Output:

Hello, Alice! You are 23 years old.

Approach

  1. Prompt the user for their name using input() and store it in a variable (a string).
  2. Prompt the user for their age using input() and convert it to an integer (or keep as string if you prefer no arithmetic operations).
  3. Use a formatted print (f-string or format) to combine name and age into a friendly greeting.

This program mainly demonstrates: input() for reading from the console, type conversion (int()), and formatted output (f-strings or format()).

Method 1 — Simple console input

Code:

# Program: Greet user by name and age (Method 1)
name = input("Enter your name: ")
age_str = input("Enter your age: ")

# Optional: convert to integer if you want to perform numeric checks
try:
    age = int(age_str)
except ValueError:
    # If conversion fails, keep the raw input and warn the user
    print("Warning: Age should be a number. Using the entered value as text.")
    age = age_str

print(f"Hello, {name}! You are {age} years old.")

Output (sample)

Explanation:

This method takes the user’s name and age from command-line arguments using the sys.argv list. It is useful when running scripts directly from the terminal without interactive prompts. The program also attempts to convert the age into an integer and safely handles invalid input. Finally, it prints a formatted greeting message using an f-string.

Method 2 — Using command-line arguments (useful for quick testing)

This method uses the sys module to accept inputs when running the script from a terminal (e.g., python greet.py Alice 23). It is helpful when automating or testing scripts.

# Program: Greet user by name and age (Method 2 - command line args)
import sys

if len(sys.argv) < 3:
    print("Usage: python greet.py <name> <age>")
    sys.exit(1)

name = sys.argv[1]
age_arg = sys.argv[2]

try:
    age = int(age_arg)
except ValueError:
    age = age_arg

print(f"Hello, {name}! You are {age} years old.")

Run :

Explanation:

In this method, the program reads the user’s name and age from command-line arguments using the sys.argv list instead of interactive input. This approach is useful when you want to run the script quickly from the terminal or automate testing. The code also converts the age to an integer safely and then prints the greeting using an f-string.

Method 3 — Web/GUI variant

If you are building a GUI (Tkinter) or a web app (Flask), the same logic applies: collect name and age from a text field, validate/convert the age, then display the greeting on the page or in a label.


Common Variations / Practice Exercises

  • Validate that age is a positive integer and re-prompt if invalid.
  • If the user is under 18, print a different message (e.g., “You are a minor”).
  • Use the birth year to calculate approximate year of birth: birth_year = current_year - age.
  • Accept full name (first + last) and format it (title case) before printing.

Notes for Beginners

  • input() always returns a string; convert to int only if you plan to do numeric operations.
  • Use try/except to gracefully handle invalid numeric input.
  • Prefer f-strings (Python 3.6+) for readable string interpolation.