Greeting and Age Check using Python

2023

In this project, we will discuss a Python code snippet that greets a person and checks their age. We'll explain each step of the code and provide the final working code at the end.


Getting User Input

To begin, we will prompt the user to enter their name using the input() function. Let's store the user's input in a variable called name:

name = input("What is your name? ")

Next, we will prompt the user to enter their place of residence. Let's store the user's input in a variable called residence:

residence = input("Where do you live? ")

Greeting and Place-Specific Message

Now, we will greet the user and display a place-specific message if they live in a particular location. In this example, let's check if the user lives in "Islandia":

print("Hello,", name)

if residence == "Islandia":
    print("Welcome to the land of enchantment!")

If the user's place of residence is "Islandia," we will print the message "Welcome to the land of enchantment!" Otherwise, we will proceed to the next step.

Age Check

We will now ask the user to enter their age and check it using an if statement. Let's store the user's input in a variable called age:

age = int(input("How old are you? "))

Next, we will perform an age check using the following conditions:

if age < 18:
    print("Too young to drive a car.")
elif age == 18:
    print("Congratulations on turning 18!")
elif age > 18:
    print("You are eligible to drive a car.")

If the user's age is less than 18, we will print the message "Too young to drive a car." If the age is exactly 18, we will print the message "Congratulations on turning 18!" If the age is greater than 18, we will print the message "You are eligible to drive a car."


Final Code

Here's the complete Python code that greets a person, displays a place-specific message, and checks their age:

name = input("What is your name? ")

residence = input("Where do you live? ")

print("Hello,", name)

if residence == "Islandia":
    print("Welcome to the land of enchantment!")

age = int(input("How old are you? "))

if age < 18:
    print("Too young to drive a car.")
elif age == 18:
    print("Congratulations on turning 18!")
elif age > 18:
    print("You are eligible to drive a car.")

That's it! You now have a Python code snippet that greets a person, displays a place-specific message, and checks their age. Feel free to modify the code or use it as a starting point for your own projects. Happy coding!

Back