|
| 1 | +from datetime import date |
| 2 | +import random |
| 3 | + |
| 4 | +def ordinal(n): |
| 5 | + if 11 <= n % 100 <= 13: |
| 6 | + return f"{n}th" |
| 7 | + if n % 10 == 1: |
| 8 | + return f"{n}st" |
| 9 | + if n % 10 == 2: |
| 10 | + return f"{n}nd" |
| 11 | + if n % 10 == 3: |
| 12 | + return f"{n}rd" |
| 13 | + return f"{n}th" |
| 14 | + |
| 15 | + |
| 16 | +def birthday_program(): |
| 17 | + print("\nBirthday Program") |
| 18 | + |
| 19 | + name = input("What is your full name?: ").strip() |
| 20 | + print(f"\nNice to meet you, {name}.") |
| 21 | + |
| 22 | + while True: |
| 23 | + try: |
| 24 | + d = int(input("\nDay you were born (1-31): ")) |
| 25 | + m = int(input("Month you were born (1-12): ")) |
| 26 | + y = int(input("Year you were born (YYYY): ")) |
| 27 | + |
| 28 | + today = date.today() |
| 29 | + birth = date(y, m, d) |
| 30 | + |
| 31 | + if birth > today: |
| 32 | + print("That birth date is in the future. Try again.") |
| 33 | + continue |
| 34 | + |
| 35 | + age = today.year - y - ((today.month, today.day) < (m, d)) |
| 36 | + days_alive = (today - birth).days |
| 37 | + |
| 38 | + next_bday = date(today.year, m, d) |
| 39 | + if next_bday < today: |
| 40 | + next_bday = date(today.year + 1, m, d) |
| 41 | + |
| 42 | + days_until = (next_bday - today).days |
| 43 | + |
| 44 | + print("\nYour birthdate:", birth.strftime("%d / %m / %Y")) |
| 45 | + print("Your age:", age) |
| 46 | + print("You have been alive for", days_alive, "days") |
| 47 | + print("That is", days_alive * 24, "hours") |
| 48 | + |
| 49 | + if days_until == 0: |
| 50 | + print(f"\n🎈 Happy {ordinal(age)} birthday! 🎈") |
| 51 | + else: |
| 52 | + print("\nYour next birthday is in:", days_until, "days") |
| 53 | + break |
| 54 | + |
| 55 | + except ValueError: |
| 56 | + print("Invalid Input. Please try again.") |
| 57 | + |
| 58 | +def days_until_event(): |
| 59 | + print("\nDays Until Event") |
| 60 | + |
| 61 | + event = input("What is the event?: ").strip() |
| 62 | + |
| 63 | + try: |
| 64 | + d = int(input("Day (1-31): ")) |
| 65 | + m = int(input("Month (1-12): ")) |
| 66 | + y = int(input("Year (YYYY): ")) |
| 67 | + |
| 68 | + today = date.today() |
| 69 | + event_date = date(y, m, d) |
| 70 | + diff = (event_date - today).days |
| 71 | + |
| 72 | + if diff < 0: |
| 73 | + print(f"\nThe event '{event}' has already passed.") |
| 74 | + elif diff == 0: |
| 75 | + print(f"\nToday is the day of '{event}'!") |
| 76 | + else: |
| 77 | + print(f"\nThere are {diff} days until '{event}'.") |
| 78 | + |
| 79 | + except ValueError: |
| 80 | + print("\nInvalid date entered.") |
| 81 | + |
| 82 | +def number_guessing_game(): |
| 83 | + print("\nNumber Guessing Game") |
| 84 | + |
| 85 | + secret = random.randint(1, 100) |
| 86 | + attempts = 0 |
| 87 | + |
| 88 | + while True: |
| 89 | + try: |
| 90 | + guess = int(input("Enter your guess (1-100): ")) |
| 91 | + attempts += 1 |
| 92 | + |
| 93 | + if guess < secret: |
| 94 | + print("Too low.\n") |
| 95 | + elif guess > secret: |
| 96 | + print("Too high.\n") |
| 97 | + else: |
| 98 | + print(f"\nCorrect! You guessed it in {attempts} attempts.") |
| 99 | + break |
| 100 | + |
| 101 | + except ValueError: |
| 102 | + print("Please enter a valid number.\n") |
| 103 | + |
| 104 | +def calculator(): |
| 105 | + print("\nCalculator") |
| 106 | + |
| 107 | + try: |
| 108 | + a = float(input("Enter 1st number: ")) |
| 109 | + op = input("Choose operation (+, -, *, /): ").strip() |
| 110 | + b = float(input("Enter 2nd number: ")) |
| 111 | + |
| 112 | + if op == "+": |
| 113 | + result = a + b |
| 114 | + elif op == "-": |
| 115 | + result = a - b |
| 116 | + elif op == "*": |
| 117 | + result = a * b |
| 118 | + elif op == "/": |
| 119 | + if b == 0: |
| 120 | + print("Cannot divide by zero.") |
| 121 | + return |
| 122 | + result = a / b |
| 123 | + else: |
| 124 | + print("Invalid operation.") |
| 125 | + return |
| 126 | + |
| 127 | + print("Result:", result) |
| 128 | + |
| 129 | + except ValueError: |
| 130 | + print("Invalid number entered.") |
| 131 | + |
| 132 | +def main_menu(): |
| 133 | + while True: |
| 134 | + print("\n=== Main Menu ===") |
| 135 | + print("1. Birthday program") |
| 136 | + print("2. Days until event") |
| 137 | + print("3. Number guessing game") |
| 138 | + print("4. Calculator") |
| 139 | + print("5. Exit") |
| 140 | + |
| 141 | + choice = input("Choose an option (1-5): ").strip() |
| 142 | + |
| 143 | + if choice == "1": |
| 144 | + birthday_program() |
| 145 | + elif choice == "2": |
| 146 | + days_until_event() |
| 147 | + elif choice == "3": |
| 148 | + number_guessing_game() |
| 149 | + elif choice == "4": |
| 150 | + calculator() |
| 151 | + elif choice == "5": |
| 152 | + print("Goodbye! Have a good day!") |
| 153 | + break |
| 154 | + else: |
| 155 | + print("Invalid choice. Try again.") |
| 156 | + |
| 157 | +main_menu() |
0 commit comments