Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing how we access values in a dictionary #8

Open
jmcrey opened this issue Dec 25, 2018 · 0 comments
Open

Changing how we access values in a dictionary #8

jmcrey opened this issue Dec 25, 2018 · 0 comments

Comments

@jmcrey
Copy link

jmcrey commented Dec 25, 2018

def most_read_book(self):
most_read = ""
times_read = 0
for book in self.books:
if self.books.values[book] > times_read:
most_read = self.books[book]
times_read = self.books.values[book]
return most_read
def highest_rated_book(self):
highest_rated = ""
highest_rating = 0
for book in self.books:
if book.get_average_rating[book] > highest_rating:
highest_rated = book
highest_rating = book.get_average_rating[book]
return highest_rated
def most_positive_user(self):
positive_user = ""
positive_score = 0
for user in self.users:
for book in User.books:
if User.books.values[book]/len(User.books) > positive_score:
positive_score = User.books.values[book]
positive_user = user
return positive_user

These functions are all really close to being correct! It performs all the operations and tests we need to determine the highest value in our dictionaries. However, we need to change just a couple of things: how we access values in a dictionary and how certain functions are called.

First, when trying to access a value in a dictionary, we need to change how we access it. Instead of writing self.books.values[book], we need to write self.books[books] to get the proper value.

P.S. values is actually a function that returns a list of all the values contained in the dictionary. For more information on dictionaries, please refer to the Codecademy lessons and/or this documentation: https://realpython.com/python-dicts/

The last thing we have to change is how we are calling the get_average_rating function. We need to change it from book.get_average_rating[book] to book.get_average_rating().

This change is necessary because get_average_rating is a function defined in our Book object. To call this function, we need to access that function from the Book object (e.g. book) and then pass it the necessary parameters. In this case, the function has no parameters, so we can simply call the function like so: book.get_average_rating().

If you have any further questions about calling class functions and/or class concepts, please refer back to the Codecademy lessons.

@jmcrey jmcrey mentioned this issue Dec 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant