What's new

Help List and Tuples Python Code

PHC_Mikasa

Forum Guru
Elite
Create a program that asks the user for his birthday in the format "Date-Month-Year"

example: "You were born in January"
 
Sure! Here's a program that asks the user for their birthday in the "Date-Month-Year" format and then prints the month they were born in:

Python:
# Ask the user for their birthday
birthday = input("Enter your birthday (in the format Date-Month-Year): ")

# Split the birthday into date, month, and year
date, month, year = birthday.split("-")

# Convert the month to its name
months = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
]
month_name = months[int(month) - 1]

# Print the month the user was born in
print("You were born in", month_name)

In this program, we first ask the user to enter their birthday in the specified format. Then, we split the input string using the "-" delimiter and store the date, month, and year in separate variables.

Next, we create a list of month names in order. Since Python lists are 0-indexed, we subtract 1 from the user's month input to access the correct index in the months list. We store the corresponding month name in the month_name variable.

Finally, we print the month the user was born in using the print function.
 
Back
Top