
The aim of this program is to store a database (dictionary) of birthdays and allow the user to add birthdays to the database if they are not present. In the book the program is written slightly differently, I experimented with the try-except block a little and wrote it with exceptions. In the book the program is basic, as it was my understanding that we would use everything we have learnt up till now. The output of the program is as follows.
Enter Name (Enter quit to Quit): Alice
inputvar : Alice
birthdays [ Alice ] : Apr 1
Enter Name (Enter quit to Quit): John
inputvar : John
I donot have that value!
Enter Now (y or n):
y
inputvar2 : y
Enter the name:
John
Enter the Birthday MMM DD:
Jan 01
Birthdays Updated!!
Enter Name (Enter quit to Quit): John
inputvar : John
birthdays [ John ] : Jan 01
Enter Name (Enter quit to Quit): Alice
inputvar : Alice
birthdays [ Alice ] : Apr 1
Enter Name (Enter quit to Quit): quit
inputvar : quit
Process finished with exit code 1
As you can see I output the control values at every step, this is purely for debugging purposes.
The Logic
The logic of the program is pretty simple to understand. We have a dictionary initialized with a few key-value pairs. The aim is to be able to view and then if needed add the birthdays of an unknown (not in the dictionary) person. All the while the program also gives you an option to quit.
The Imports
So as far as the imports go, I have imported the nameof() method from the varname library. This is to purely print the name of the variables.
print(nameof(inputvar),":",inputvar,sep="\t")
Next, I have imported the random and sys packages. The random package I had originally imported to allow me to make random choices between all the entries in the dictionary “birthdays“, but as I proceeded to understand and write the program I lost all use for this package so feel free not to import this package. The sys package, as you might remember from the previous programs, is to modify the exit code of the program to indicate successful completion. In this case the exit code is 1.
sys.exit(1)
Initialization
Now as per the example in the book I initialize the dictionary “birthdays” with the appropriate key-value pairs, like so:
birthdays = {
"Alice": "Apr 1",
"Bob": "Dec 12",
"Carol": "Mar 4"
}
So now we can see that the “birthdays” dictionary has been initialized with three key-value pairs. As mentioned in the logic these key-value pairs are now known and we should be able to print them as requested.
Also an important initialization is the program control variable deciding whether the main loop should be run or not, initially it is set to True so we at least have one pass through the main program loop.
choice = True
The Main Loop
Once I understood the logic , I leveraged things I have learnt up to now to make this my own rendition of the program. After initialization in order to keep the program running until expressly quit i.e. by specifically typing “quit”, we encompass the main code in a while loop. Remember here we check the while loop against the variable choice, which we initialized to True ensuring one pass through of the main program.
while choice:
In the book, the author uses a series of if-else conditions to achieve the same result. But I have leveraged the try-except conditions to achieve the same end.
After opening the while loop, here’s where I experimented with the try-except control structure. First off, we ask the user to enter a name or type “quit” to exit the program. Moving on, I encompass this option within a try-except control sequence and catch a KeyboardInterrupt which will directly exit the program.
except KeyboardInterrupt:
print("Ending!!!")
Next, we proceed with an if-else control structure in this case to modify the choice so we can terminate the running of the program. Here you will find a new concept, break, this control statement terminates the running of the main while loop.
if inputvar == "quit" or inputvar == "Quit" or inputvar == "QUIT":
choice = False
break
else:
choice = True
If the program is terminated the control moves directly from within this first choice while loop and directly to after the while loop has ended. Once the name is entered into inputvar, I directly move to printing the value of birthdays with inputvar as a key. Here what is interesting is the use of the try-except control structure. Instead, of putting a if-else condition, here I used the exception KeyError which is triggered when the entered key does not have a matching key-value pair in the dictionary. Here it prints the value and if a KeyError is thrown the control sequence directly goes to adding a new key-value pair to the dictionary “birthdays”.
try:
print(nameof(birthdays),"[",inputvar,"]",":",birthdays[inputvar],sep="\t")
except KeyError:
print("I donot have that value! \n Enter Now (y or n):")
inputvar2 = input()
print(nameof(inputvar2), ":", inputvar2, sep="\t")
if inputvar2 == "y" or inputvar2 == "Y":
try:
print("Enter the name:")
ipname = input()
print("Enter the Birthday MMM DD:")
ipdate = input()
birthdays[ipname] = ipdate
print("Birthdays Updated!!")
except KeyboardInterrupt:
print("Exiting!...")
else:
print("Exiting!!")
During the addition of a key-value pair I again use the control structure try-except to check for a KeyboardInterrupt which will directly exit the program.
Final Thoughts
It was really fun to try using the try-except control sequence. During the duration of this project I learned how to leverage the exceptions to achieve a similar functionality to the if-else conditional statements. Again an improvement could be using functions to achieve the same result. Definitely one to try again.
Good Job