24
loading...
This website collects cookies to deliver better user experience
Animal Registry | ||
---|---|---|
ID | Scientific Name | Common Name |
3Y99 | Canis Lupus Familiaris | Siberian Husky |
MT5R | Cebinae | Capuchin Monkey |
8D6U | Octopoda | Octopus |
YG0E | Anthophila | Bee |
JN14 | Dinastinae | Rhinoceros Beetle |
WVTA | Python Regius | Ball Python |
HAZP | Gorilla | Gorilla |
You must install PrettyTable via Pip.
Try with:
pip install prettytable
from prettytable import PrettyTable
import random
import string
import sys
animal_dic = {}
if __name__ == "__main__":
main()
An explanation of how if _name_ == "_main_" works is beyond the scope of this article.
# ~~~~~~~~~~~~~~~~~~ Functions(): ~~~~~~~~~~~~~~~~~~~
def main():
# ~~~~~~~~~~~~~~ User's choise ~~~~~~~~~~~~~~~
while True:
instrucctions()
user_input = input("\nWhat do you want to do? "
"(a, d, u, l, e): ").lower()
if user_input == "a":
add_animal()
elif user_input == "d":
delete_animal()
elif user_input == "u":
update_animal()
elif user_input == "l":
print_register()
elif user_input == "e":
exit_program()
elif not user_input:
print("please, enter something!")
# ~~~~~~~~~~~~~ Functions ~~~~~~~~~~~~~~
def instrucctions():
print('\nAnimal registry program:'
'\n1: Enter A or a to add new animal.'
'\n2: Enter D or d to delete a animal'
'\n3: Enter U or u to update animal.'
'\n4: Enter L or l to check list of animals. '
'\n5: Enter E or e to exit the program.')
def print_registry():
x = PrettyTable(["ID", "Scientific Name", "Common Name"])
for animal_data in animal_dic:
x.add_row([animal_data, animal_dic[animal_data]["scientific_name"],
animal_dic[animal_data]["common_name"]])
print(x.get_string(title="Animal registry"))
{'name': 'Adam'}
{'name': 'Adam', 'last': 'Smith'}
{0: {'book': 'Wealth of Nations', 'author': 'Adam Smith'},
1: {'book': 'Economic Sophisms',' author': 'Frédéric Bastiat'}}
for animal_data in animal_dic:
x.add_row([animal_data, animal_dic[animal_data]["scientific_name"],
animal_dic[animal_data]["common_name"]])
x.add_row([animal_data, animal_dic[animal_data]["scientific_name"],
animal_dic[animal_data]["common_name"]])
print(x.get_string(title="Animal registry"))
def random_id():
random_string = ''.join(random.choices(string.ascii_uppercase
+ string.digits, k=4))
return random_string
An explanation of how this function works is beyond the scope of this article.
def add_animal():
animal_id = random_id()
scientific_name = input("\nPlease enter the scientific name: ").title()
common_name = input("\nPlease enter the common name: ").title()
data = {animal_id: {'scientific_name': scientific_name,
'common_name': common_name}}
if not scientific_name and not common_name:
print("You must write something!")
else:
animal_dic.update(data)
data = {animal_id: {'scientific_name': scientific_name,
'common_name': common_name}}
We update our nested dictionary and store it in a variable called "data". So:
animal_id is the random ID that our function returns. It is the external key of this
nested dictionary.
'scientific_name': which is in quotation marks, is the internal key.
scientific_name: which is a variable, is the internal value. Entered by the user.
The next block of code sets a condition where if the user does not enter either a scientific name or a common name, print a message that he/she must do so.
Finally, in the 'else': if at least one of the two input() have been entered, we will update the "animal_dic" with the data entered by the user.
def delete_animal():
animal_id = input("\nEnter the animal ID you want delete: ").upper()
if animal_id in animal_dic:
choice = input("Delete (y/n)").lower()
if choice == "yes" or choice == "y":
del animal_dic[animal_id]
print(f"{animal_id} registry has been deleted!")
else:
print("ID not found. Check list pressing 'L'")
def update_animal():
animal_id = input("\nEnter the animal ID you want update: ").upper()
# If external key in dictionary, if key is equal to ID (animal_id)
for animal in animal_dic:
if animal == animal_id:
choice = input(f"Update registry {animal_id}? (y/n): ").lower()
if choice == "yes" or choice == "y":
# Changing names
scientific_name = input("Write a new scientific name: ").title()
common_name = input("Write a new common name: ").title()
if not scientific_name and not common_name:
print("You must write something!")
else:
# Updating
animal_dic[animal]['scientific_name'] = scientific_name
animal_dic[animal]['common_name'] = common_name
print("registry updated!")
print_registry()
else:
print("ID not found. Check list pressing 'L'")
def exit_program():
sys.exit("Goodbye!")