Loading lessons...
Project 1: Greeting Machine
Project 1: Greeting Machine
Your first Python program: ask for a name and age, then greet the user with a message.
The goal
Read the user's name and age with input(), then print a friendly message that says how old they will be next year.
What you practice
print()outputinput()user input- Casting with
int() - f-string formatting
Starter code
name = input("What is your name? ")
age = int(input("How old are you? "))
next_year = age + 1
print(f"Hello {name}, next year you will be {next_year}!")
Step-by-step
- Run the starter and answer the questions.
- Add a line that also prints
type(age)to see whatinput()returns. - Wrap the age conversion in a try/except that catches a bad number.
- Print a goodbye message at the end.
- Re-run and test with both valid and invalid ages.
Checklist
- [ ] The name is read with input()
- [ ] The age is converted with int()
- [ ] The message uses an f-string
- [ ] Next year's age is correct
- [ ] A try/except handles bad input
- [ ] The program runs without errors
TL;DR
input()always returns text.- Convert with int() before doing math.
- f-strings make messages clean.