Class 2 Lab 2 ‐ Basic Conditions - Justin-Boyd/Python-Class GitHub Wiki
Step 1: Create two variables, name, and age, and use the input() function to get user input.
name = input("What is your name? ")
age = input("What is your age? ")
Step 2: Use the int() function to cast the variable age from a String data type to an integer data type.
age = int(age)
Step 3: Print a greeting containing the user’s name and age. Use the format method to insert the variables.
print("Hello {}, your age is {}".format(name, age))
Step 4: Create an if-else statement to check if the user is old enough to purchase an alcoholic beverage and print an appropriate message.
if age >= 21:
print("You can buy an alcoholic beverage")
else:
print("You cannot buy an alcoholic beverage")
Full Code
name = input("What is your name? ")
age = input("What is your age? ")
age = int(age)
print("Hello {}, your age is {}".format(name, age))
if age >= 21:
print("You can buy an alcoholic beverage")
else:
print("You cannot buy an alcoholic beverage")
input("\nPress 'Enter' to exit this program.")