MorneTer / pwp-capstones

0 stars 0 forks source link

add_book_to_user will error out if the user does not exist #5

Open jmcrey opened 6 years ago

jmcrey commented 6 years ago

https://github.com/MorneTer/pwp-capstones/blob/04fefad0f4be95b01417d24acbe8c23b5f400dee/Submit/TomeRater.py#L138-L151

Note that this function will error out if the user does not exist. To prevent this, we should use the get method to test whether the user exists. For example, we can do the following:

     user = self.users.get(email)

     if user:

The get function will return the value at the key if the key is in the dictionary; if the key is not in the dictionary, it will return None. So, if the email is in the users dictionary, then the variable user will have a value; if not, then user will be None, which will make our if statement return False.

Note that our prior implementation would throw a KeyError if the email was not already in the dictionary.