Hi again, today I’ll show you how you can magically tell anyone what day of the week they were born! No matter how long ago, even if they even didn’t know 🙂 The magic is in coding (I’ll use Python here) and its calendar module. The complete source is below along with a challenge at end for you to try.
The key is to use the calendar module of Python and use its method called weekday(). Which takes 3 parameters for it to return a integer for the weekday starting Monday. That is, if it returns 0, it’s Monday. Obviously you have to import the module and the calendar module as shown in the code below .
It won’t return the weekday names, so you have to transform it to a weekday name as you like. I do it using tuple feature of Python, but there are many other ways to do it. Tuple was the cleanest way I think. Rest of the code is getting user inputs. The inputs are entered as strings so to do any math operations, you have to convert them to a number such as turning them into integers using int(string) function. Super easy!
Full Source Code & Output
- I purposely left out the error checks. Add a check so that if the input is NOT a string version of a digit, you catch it…and either exit with a warning or let the user try again. Hint: Use isnumeric() in Python, but if you use another language, same idea…just different name of the function. To exit application, you can use sys.exit() in Python and if you do, you have to import the sys module since the exit function is in that module apparently.
- Use something other than a tuple to convert the day number to a name.