Jul-03-2020, 09:01 AM
(This post was last modified: Jul-03-2020, 10:54 PM by Drone4four.)
I’m learning how to use the function
Here is the exercise that I am grappling with:
Here is the sample code:
When I started this exercise, I immediately found the official Python doc which describes namedtuple() here.
The doc does a terrific job demonstrating how to use
Without providing the solution for me, what hints or comments would you people be able to suggest to help nudge me in the right direction?
I came up with my shell ‘solution’ (above) based on Udemy courses I’ve been taking by Fred Batiste and Jose Portilla. I can’t share the specific lectures/lessons on dictionary manipulation because it is paywalled. Jose Portilla’s course in particular primarily uses Jupyter Notebooks which kinda explains why my Python shell chops are superior when compared to me trying to write actual scripts.
For more about this exercise and what I am trying to achieve, see this:
namedtuple()
found in the collections module / library.Here is the exercise that I am grappling with:
Quote:Calculate the amount of points awarded on PyBites given the ninja_belts dictionary, formula: belt score x belt owners (aka ninjas). Make your code generic so if we update ninja_belts to include more belts (which we do in the tests) it will still work. Ah and you can get score and ninjas from the namedtuple with nice attribute access: belt score / belt.ninjas (reason why we get you familiar with namedtuple here, because we love them and use them all over the place!)
Return the total number of points int from the function.
Here is the sample code:
from collections import namedtuple BeltStats = namedtuple('BeltStats', 'score ninjas') ninja_belts = {'yellow': BeltStats(50, 11), 'orange': BeltStats(100, 7), 'green': BeltStats(175, 1), 'blue': BeltStats(250, 5)}The expected end result for this exercise is simply: 2675
When I started this exercise, I immediately found the official Python doc which describes namedtuple() here.
The doc does a terrific job demonstrating how to use
namedtuple()
’s in Python’s interactive REPL shell. Based on the doc, I adapted the namedtuple()
demo to come up with the expected output for my exercise using the following:>>> from collections import namedtuple >>> BeltStats = namedtuple('BeltStats', 'score ninjas') >>> ninja_belts = {'yellow': BeltStats(50, 11), ... 'orange': BeltStats(100, 7), ... 'green': BeltStats(175, 1), ... 'blue': BeltStats(250, 5)} >>> BY = ninja_belts['yellow'] >>> BO = ninja_belts['orange'] >>> BG = ninja_belts['green'] >>> BB = ninja_belts['blue'] >>> test = (BY[0]*BY[1] + BO[0]*BO[1] + BG[0]*BG[1] + BB[0]*BB[1]) >>> print(test) 2675Notice the result at the very end there? It says: 2675. This matches the expected output. Hooray! I did it? Well, almost. I successfully manipulated the dictionary to calculate the desired output. My problem is how to reformat those operations into a function inside an actual script to automate that output. I’m completely lost trying to do that.
Without providing the solution for me, what hints or comments would you people be able to suggest to help nudge me in the right direction?
I came up with my shell ‘solution’ (above) based on Udemy courses I’ve been taking by Fred Batiste and Jose Portilla. I can’t share the specific lectures/lessons on dictionary manipulation because it is paywalled. Jose Portilla’s course in particular primarily uses Jupyter Notebooks which kinda explains why my Python shell chops are superior when compared to me trying to write actual scripts.
For more about this exercise and what I am trying to achieve, see this:
Quote:In this Bite you calculate the total amount of points earned with Ninja Belts by accessing the given ninja_belts dict.
You learn how to access score and ninjas (= amount of belt owners) from no less than a namedtuple (if you're new to them, check out the basic Point example in the docs).
Why a namedtuple, you did not even mention a tuple yet?!
Good point, well in our Bites we might actually use them even more so let's get to know them here (if you have a free evening read up on the collections module as well and thank us later).
The function returns the total score int. You learn to write generic code because we test for an updated ninja_belts dict as well, see the TESTS tab.