# Rate my Python code/project

**URL:** <https://forum.freecodecamp.org/t/rate-my-python-code-project/476090>\
**Category:** I Built This\
**Created:** [September 1, 2021, 5:39am UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090 "2021-09-01T05:39:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardoaguilarm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ricardoaguilarm/32/121459_2.png) [@ricardoaguilarm](https://forum.freecodecamp.org/u/ricardoaguilarm)\
**Post date:** [September 1, 2021, 5:39am UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/1 "2021-09-01T05:39:13Z")

</div>

Hi folks! I’m new on Python so, recently I create this little program. You can register an animal in a list (create), update, delete, and check that list.  
Do you have any recommendations or see something weird in my code, or something that can be improved?  
Thanks in advance.

> If you want run this program remember install PrettyTable.

```Python
from prettytable import PrettyTable
import random
import string
import sys

animal_dic = {}

print('\nAnimal register 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.')

# ~~~~~~~~~~~~~~~~~~Functions():~~~~~~~~~~~~~~~~~~~
def print_register():
    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 Register"))

def random_id():
    random_string = ''.join(random.choices(string.ascii_uppercase
                                           + string.digits, k=4))
    return random_string

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)

def delete_animal():
    animal_id = input("\nEnter the animal ID you want delete: ").upper()
    try:
        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} register has been deleted!")
        else:
            print("ID not found. Check list pressing 'L'")
    except Exception:
        print("Something bad happend.")

def update_animal():
    animal_id = input("\nEnter the animal ID you want update: ").upper()
    try:
        # If key in dictionary, if key is equal to ID (animal_id)
        for animal in animal_dic:
            if animal == animal_id:
                choice = input(f"Update register {animal_id}? (y/n): ").lower()
                if choice == "yes" or choice == "y":
                    # Changing names
                    scientific_name = input("Write a new scientific name: ")
                    animal_dic[animal]['scientific_name'] = scientific_name
                    common_name = input("Write a new common name: ")
                    animal_dic[animal]['common_name'] = common_name
                    print("Register updated!")
                    print_register()
                else:
                    print("ID not found. Check list pressing 'L'")
    except Exception:
        print("Something bad happend.")

def exit_program():
    sys.exit("Goodbye!")

# ~~~~~~~~~~~~~~User's choise~~~~~~~~~~~~~~~
while True:
    user_input = input("\nwhat do you want to do? (a, d, u, e, l): ").lower()
    if user_input == "a":
        add_animal()
    elif user_input == "d":
        delete_animal()
    elif user_input == "u":
        update_animal()
    elif user_input == "e":
        exit_program()
    elif user_input == "l":
        print_register()
    elif not user_input:
        print("please, enter something!")

```

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [September 1, 2021, 7:23am UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/2 "2021-09-01T07:23:33Z")

</div>

I’d start with asking what do you think about it? What was harder and easier than you expected while writing this? Any specific issues during that, or particularly hard parts? Is there anything that feels off to you? Do you have any suspected places where code might be improved or in what areas that improvement might be?

---

<div class="post-metadata">

**Author:** ![ricardoaguilarm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ricardoaguilarm/32/121459_2.png) [@ricardoaguilarm](https://forum.freecodecamp.org/u/ricardoaguilarm)\
**Post date:** [September 1, 2021, 11:08am UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/3 "2021-09-01T11:08:15Z")

</div>

It was difficult find a way to store a data that can be bind one single “key” to more that two values, then someone here say "are you ever heard about “nested dictionaries” and that was the main “Oh! i got it!”. Then, was a little challenge find the way to access to certain values. I still handle my insecurities/perfection in order to make my code more “elegant”. So, always i’m looking for recommendation, maybe someone has a better or simpler way to make a thing.  
My main concern is about the way I’m handling the Exception.

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [September 1, 2021, 11:41am UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/4 "2021-09-01T11:41:56Z")

</div>

There’s few things that can be looked at, but exceptions is a nice place to start.

Considering `delete_animal` function, are you expecting any specific issue which might raise exception there?

Usually it’s better to catch only those exceptions that in fact are expected in some part of code. Catching whole `Exception` class makes unclear what actually was an issue. This includes multiple exceptions that are raised only in some abnormal situation - for example like memory or system issue. Sure there’s information printed that something happened, but what and why remains unknown.

---

<div class="post-metadata">

**Author:** ![ricardoaguilarm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ricardoaguilarm/32/121459_2.png) [@ricardoaguilarm](https://forum.freecodecamp.org/u/ricardoaguilarm)\
**Post date:** [September 2, 2021, 2:13pm UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/5 "2021-09-02T14:13:37Z")

</div>

You are totally right! Thanks a tons! I will be aware of that the next time.

---

<div class="post-metadata">

**Author:** ![ricardoaguilarm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ricardoaguilarm/32/121459_2.png) [@ricardoaguilarm](https://forum.freecodecamp.org/u/ricardoaguilarm)\
**Post date:** [September 6, 2021, 12:34pm UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/6 "2021-09-06T12:34:34Z")

</div>

Btw, should I write an  
if **name** == “ **main** ”:  
Then the while True loop (the last part of the code)? I’m very confusing if I always have to write that “if **name** …” Even in small programs like this.

---

<div class="post-metadata">

**Author:** ![ricardoaguilarm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ricardoaguilarm/32/121459_2.png) [@ricardoaguilarm](https://forum.freecodecamp.org/u/ricardoaguilarm)\
**Post date:** [September 6, 2021, 12:53pm UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/7 "2021-09-06T12:53:04Z")

</div>

Like this. Works good, and it’s better, right?

```python
if __name__ == " __main__":
    animal_dic = {}

    # ~~~~~~~~~~~~~~User's choise~~~~~~~~~~~~~~~
    while True:
        instrucctions()
        user_input = input("\nWhat do you want to do? "
                           "(a, d, u, e, l): ").lower()
        if user_input == "a":
            add_animal()
        elif user_input == "d":
            delete_animal()
        elif user_input == "u":
            update_animal()
        elif user_input == "e":
            exit_program()
        elif user_input == "l":
            print_register()
        elif not user_input:
            print("please, enter something!")

```

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [September 6, 2021, 1:55pm UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/8 "2021-09-06T13:55:23Z")

</div>

That depends how one look at it. If you don’t plan to import functions from this file, or the whole file to some other code, then it will not matter much really.

What this condition does is ensure that code in it will be run only if file is run as standalone (ie. it’s not imported by some other file).

---

<div class="post-metadata">

**Author:** ![ricardoaguilarm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ricardoaguilarm/32/121459_2.png) [@ricardoaguilarm](https://forum.freecodecamp.org/u/ricardoaguilarm)\
**Post date:** [September 6, 2021, 4:26pm UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/9 "2021-09-06T16:26:30Z")

</div>

> [@sanity](#):
>
> What this condition does is ensure that code in it will be run only if file is run as standalone (ie. it’s not imported by some other file).

Thanks again! I Just wanna follow best practice!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [March 8, 2022, 4:26am UTC](https://forum.freecodecamp.org/t/rate-my-python-code-project/476090/10 "2022-03-08T04:26:46Z")

</div>

Closed on March 8, 2022, 4:26am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
