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.
input() and store it in a variable (a string).input() and convert it to an integer (or keep as string if you prefer no arithmetic operations).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()).
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)
Enter your name: Alice
Enter your age: 23
Hello, Alice! You are 23 years old.
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.
This method uses the
sysmodule 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 :
$ python greet.py Alice 23
Hello, Alice! You are 23 years old.
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.
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.
age is a positive integer and re-prompt if invalid.birth_year = current_year - age.input() always returns a string; convert to int only if you plan to do numeric operations.try/except to gracefully handle invalid numeric input.