Lab 4: if-else Statements
Program 1: Oldest and Youngest
'''
Author name: <Your name here>
This program reads ages of 3 different users and determine
the oldest and youngest among them. Display the results.
'''
# Read the three ages from the users.
age1 = eval(input("Enter the age of user one: "))
age2 = eval(input("Enter the age of user two: "))
age3 = eval(input("Enter the age of user three: "))
# If the age is the max age, then print out the result.
if age1 >= age2 and age1 >= age3:
print("The first age is the maximum age of", age1)
if age2 >= age1 and age2 >= age3:
print("The second age is the maximum age of", age2)
if age3 >= age1 and age3 >= age2:
print("The third age is the maximum age of", age3)
# If the age is the min age, then print out the result.
if age1 <= age2 and age1 <= age3:
print("The first age is the minimum age of", age1)
if age2 <= age1 and age2 <= age3:
print("The second age is the minimum age of", age2)
if age3 <= age1 and age3 <= age2:
print("The third age is the minimum age of", age3)
or
'''
Author name: <Your name here>
This program reads ages of 3 different users and determine
the oldest and youngest among them. Display the results.
'''
# Read the three ages from the users.
age1 = eval(input("Enter the age of user one: "))
age2 = eval(input("Enter the age of user two: "))
age3 = eval(input("Enter the age of user three: "))
# Get the max age
max_age = max(age1, age2, age3)
# Get the min age
min_age = min(age1, age2, age3)
# If the age is the max age, then print out the result.
if age1 == max_age:
print("The first age is the maximum age of", max_age)
if age2 == max_age:
print("The second age is the maximum age of", max_age)
if age3 == max_age:
print("The third age is the maximum age of", max_age)
# If the age is the min age, then print out the result.
if age1 == min_age:
print("The first age is the minimum age of", min_age)
if age2 == min_age:
print("The second age is the minimum age of", min_age)
if age3 == min_age:
print("The third age is the minimum age of", min_age)
Program 2: Find future days
'''
Author name: <Your name here>
This program prompts the user to
enter an integer for today’s day of the week
(Sunday is 0, Monday is 1, ..., and Saturday is 6).
Then prompt the user to enter the number of days after
today for a future day and display the future day of the week.
'''
# Ask the user to enter today's number
today = eval(input("Enter today’s day: "))
# Ask the user to enter the number of day elapsed since today
elapsed = eval(input("Enter the number of days elapsed since today: "))
# Calculate the future day by using remained 7
future_day = (today + elapsed) % 7
# Create a string to store the day of the week
today_string = ""
if today == 0:
today_string = "Sunday"
elif today == 1:
today_string = "Monday"
elif today == 2:
today_string = "Tuesday"
elif today == 3:
today_string = "Wednesday"
elif today == 4:
today_string = "Thursday"
elif today == 5:
today_string = "Friday"
else:
today_string = "Saturday"
# Create a string to store the day of the week
future_day_string = ""
if future_day == 0:
future_day_string = "Sunday"
elif future_day == 1:
future_day_string = "Monday"
elif future_day == 2:
future_day_string = "Tuesday"
elif future_day == 3:
future_day_string = "Wednesday"
elif future_day == 4:
future_day_string = "Thursday"
elif future_day == 5:
future_day_string = "Friday"
else:
future_day_string = "Saturday"
# Print the results
print("Today is", today_string, "and the future day is", future_day_string);
Program 3: Divisible By
'''
Author name: <Your name here>
This program reads a number from the user and checks the following:
a. Number is divisible by 2 and 3
b. Number is divisible by 2 or 3
c. Number is divisible by 2 or 3 but not both
'''
# Read a number from the user
num = eval(input("Enter a number: "))
# Check if Number is divisible by 2 and 3
if num % 2 == 0 and num % 3 == 0:
print("Number is divisible by 2 and 3")
# Else check if Number is divisible by 2 or 3
elif num % 2 == 0 or num % 3 == 0:
print("Number is divisible by 2 or 3")
# Check if Number is divisible by 2 or 3 but not both
if not num % 2 == 0 and num % 3 == 0:
print("Number is divisible by 2 or 3 but not both")