Skip to content

Instantly share code, notes, and snippets.

@erikw
Last active February 28, 2025 15:54
Show Gist options
  • Save erikw/a24d46893148702783043ac8a698ee44 to your computer and use it in GitHub Desktop.
Save erikw/a24d46893148702783043ac8a698ee44 to your computer and use it in GitHub Desktop.
[CS50x Python Aryaloka] Workshop: Refactoring

[CS50x Python Aryaloka] Workshop: Refactoring

We stared with the file bithday-ugly.py and the refactored version is in bithday-refactored.py with the two extra modules we created after it.

def message_header():
return "\n🎈🎈🎈🎈🎈🎈 Here is some birthday stats for you. 🎈🎈🎈🎈🎈🎈"
def message_for_year(year):
if year <= 1950:
return "πŸ‘΄ Wow, you are old now!"
else:
return "πŸ‘Ά You are still young, enjoy!"
def message_for_month(month):
msg = f"Oh, you are born in month number {month}: "
match month:
case 1:
msg += "πŸ“… January, known as the Long Nights."
case 2:
msg += "πŸ“… February, known as the Snow Melt."
case 3:
msg += "πŸ“… March, known as the New Blossom."
case 4:
msg += "πŸ“… April, known as the Old Blossom."
case 5:
msg += "πŸ“… May, known as the Kord's Tempest."
case 6:
msg += "πŸ“… June, known as the Fresh Hay."
case 7:
msg += "πŸ“… July, known as the Pelor's Blessing."
case 8:
msg += "πŸ“… August, known as the Golden Harvest."
case 9:
msg += "πŸ“… September, known as the Pelor's Bounty."
case 10:
msg += "πŸ“… October, known as the Leaf Fall."
case 11:
msg += "πŸ“… November, known as the Sehanine."
case 12:
msg += "πŸ“… December, known as the Dark Frost."
return msg
def message_for_day(day):
msg = "Your bith-day is "
if day % 2 == 0:
msg += "even"
else:
msg += "odd"
msg += "."
return msg
import sys
from date_validation2 import validate_date
from birthday_messages import message_header, message_for_year, message_for_month, message_for_day
QUESTION ="Hello dear user, please tell me what year, month and day were you born [yyyy-mm-dd] in. Then I will tell you something about it. Input: "
def get_input():
while True:
try:
date = input(QUESTION)
year, month, day = date.split("-")
year = int(year)
month = int(month)
day = int(day)
break
except ValueError as e:
print("Try again. Input in format yyyy-mm-dd", file=sys.stderr)
return [year, month, day]
def main():
year, month, day = get_input()
validate_date(year, month, day)
print(message_header())
print(message_for_year(year))
print(message_for_month(month))
print(message_for_day(day))
if __name__ == "__main__":
main()
import sys
while True:
try:
date = input("Hello dear user, please tell me what year, month and day were you born [yyyy-mm-dd] in. Then I will tell you something about it. Input: ")
year, month, day = date.split("-")
year = int(year)
month = int(month)
day = int(day)
break
except ValueError as e:
print("Try again. Input in format yyyy-mm-dd", file=sys.stderr)
if not (0 <= year <= 9999):
raise ValueError("Invalid year range")
if not (1 <= month <= 12):
raise ValueError("Invalid month range")
if not (1 <= day <= 31):
raise ValueError("Invalid day range")
if year <= 1950:
print("πŸ‘΄ Wow, you are old now!")
else:
print("πŸ‘Ά You are still young, enjoy!")
print()
print("🎈🎈🎈🎈🎈🎈 Here is some birthday stats for you. 🎈🎈🎈🎈🎈🎈")
print(f"Oh, you are born in month number {month}: ", end="")
match month:
case 1:
print("πŸ“… January, known as the Long Nights.")
case 2:
print("πŸ“… February, known as the Snow Melt.")
case 3:
print("πŸ“… March, known as the New Blossom.")
case 4:
print("πŸ“… April, known as the Old Blossom.")
case 5:
print("πŸ“… May, known as the Kord's Tempest.")
case 6:
print("πŸ“… June, known as the Fresh Hay.")
case 7:
print("πŸ“… July, known as the Pelor's Blessing.")
case 8:
print("πŸ“… August, known as the Golden Harvest.")
case 9:
print("πŸ“… September, known as the Pelor's Bounty.")
case 10:
print("πŸ“… October, known as the Leaf Fall.")
case 11:
print("πŸ“… November, known as the Sehanine.")
case 12:
print("πŸ“… December, known as the Dark Frost.")
if day % 2 == 0:
print("Your bith-day is even.")
else:
print("Your bith-day is odd.")
def validate_year(year):
if not (0 <= year <= 9999):
raise ValueError("Invalid year range")
def validate_month(month):
if not (1 <= month <= 12):
raise ValueError("Invalid month range")
def validate_day(day):
if not (1 <= day <= 31):
raise ValueError("Invalid day range")
def validate_date(year, month, day):
validate_year(year)
validate_month(month)
validate_day(day)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment