Need help with a calendar

I’m trying to make a calendar that asks the user for their birthday and it shows the month that they were born with their birthday highlighted. But right now, it just highlights every instance of that number. Wondering if there’s a way to fix that.
Repl link:

code snippet

The str.replace() command replaces every instance in a string.
If there are spaces around the number you want to highlight in the formatted calendar, you could replace the command:

highlighted_calendar_str = calendar_str.replace(str(d), "\033[91m" + str(d) + "\033[0m"). (line 21)

highlighted_calendar_str = calendar_str.replace(" " + str(d) + " ", " " + "\033[91m" + str(d) + "\033[0m" + " ").

This way, it only replaces the number that is on its own in the calendar (for one-digit dates).

If you need clarification, I can give it. :slightly_smiling_face:

I doubt it’s easier, but to be more efficient, you could use google api services to do all the work for both parties.

How would that help here? They’re not trying to use external calendar services.

Thank you for the help, is there any way that it could work for two-digit dates?

It would already work for two-digit dates. The spaces just allow one-digit dates to be detected on their own, and not as part of a two-digit date.

Before spaces:
Search: “1”

[1] … [1][1] … 2[1] … 3[1]

After spaces:
Search: “1”

[1] … 11 … 21 … 31

For some reason, the code isn’t highlighting the date, even when I replace the escape sequences with brackets…

Like already said here the problem with the str.replace() is that he replaces all instances of the substring, so you need to modifyto only target specific days.

What I do recommend here is for you to split the calendar into weeks and compile a regex to find the exact day. You can use \b in the regex to prevents partial matches and re.escape(str(d)) to escape the day number d.

So it would result in something like this:

def birthday(y, m, d):
    # Creating the calendar
    c = calendar.TextCalendar(calendar.SUNDAY)
    calendar_str = c.formatmonth(y, m)

    # Split the calendar
    weeks = calendar_str.split('\n')

    # Compile the regex
    day_regex = re.compile(r'\b' + re.escape(str(d)) + r'\b')

    # Process each week to find and replace the specific day with highlights
    for i in range(len(weeks)):
            weeks[i] = day_regex.sub("\033[91m" + str(d) + "\033[0m", weeks[i])

    # Join the weeks back into a single string
    highlighted_calendar_str = '\n'.join(weeks)

    # Print the highlighted calendar

This will not precisely solve the issue because in this approach you replace every instance of the day number in the calendar string, leading to the problem that op described.

You try to mitigate the issue by adding spaces around d , which will reduce incorrect matches… buuuut it will only work if d is neither at the beginning nor the end of a line, and it fails for single-digit days at the start or end of weeks because they might only have one space or be adjacent to other characters like newline characters.

Okay. I am sorry that I was unable to provide adequate help. :saluting_face:

Oh no, it’s totally ok, I was just pointing out why it didn’t work in your case

All right. I just didn’t (still don’t :sweat_smile:) know about regex expressions, so I didn’t know that was a possibility.