Hi there, it looks like you have a spelling error when you defined Kimberly. An extra “l” in ‘availability’.
try removing that and see if it works
Can Anyone guide me please.
I have downloaded miniconda and python 3.9 and pycharm and abruptly goblins file but I am not able to find the project.
I dont know how to use terminal
what are the next steps .
SomeOne please help me out
Hi, I’m doing this exercise and I am stumbling. I have reached the point where I create a dictionary called kimberly, and add the name and availability. Here is my code so far:
gamers = []
def add_gamer(gamer, gamers_list):
if gamer.get("name") and gamer.get("availability"):
gamers_list.append(gamer)
else:
print("Gamer missing critical information")
kimberly = {
'name': "Kimberly Warner",
'availability': ["Monday", "Tuesday", "Friday"]
}
add_gamer(kimberly, gamers)
print(gamers)
When I try to call this function (add_gamer), I get this:
NameError Traceback (most recent call last)
<ipython-input-20-a63affcc1778> in <module>
4 }
5
----> 6 add_gamer(kimberly, gamers)
7 print(gamers)
NameError: name 'add_gamer' is not defined
And what’s REALLY crazy is that in the solution, when I try to call the function there, which looks just like mine, I get the same name error OVER THERE too.
I can see the errors have tracebacks mentioning ipython. Make sure you run that function definition before the lines below it. If this is jupyter then make sure you run the cell containing the function definition before calling the function.
If you restart the kernel the text remains in the cells but the actual objects would be forgotten (so run every cell necessary in order if that’s the case).
Getting stuck pretty early on.
The project asks:
Instructions
Write a function called
calculate_availability
that takes a list of gamers as an argumentgamers_list
and a frequency tableavailable_frequency
. The function should iterate through each gamer ingamers_list
and iterate through each day in the gamer’s availability. For each day in the gamer’s availability, add one to that date on the frequency table.
What is a frequency table?
How do we work with one?
I don’t remember learning anything about frequency tables up to this point.
I don’t mind googling it so much (I’ve googled other concepts) but frequency tables seems like something we ought to have had a lesson or two on before being asked to work with them.
Okay I scanned through some other solutions here briefly and surmised that a frequency table could just be a dictionary with keys of the items you want to know the frequency of and values as the frequency of those items.
I am a little bit proud of this function as it’s probably the most complex function I’ve written that worked straight away without any errors.
Now that being said I’m sure there are more elegant solutions and I’m sure that someone will come along and tell me that this doesn’t actually work.
Here it is anyway:
gamers = [{'name': 'Thomas Nelson', 'availability': ['Tuesday', 'Thursday', 'Saturday']}, {'name': 'Joyce Sellers', 'availability': ['Monday', 'Wednesday', 'Friday', 'Saturday']}, {'name': 'Michelle Reyes', 'availability': ['Wednesday', 'Thursday', 'Sunday']}, {'name': 'Stephen Adams', 'availability': ['Thursday', 'Saturday']}, {'name': 'Joanne Lynn', 'availability': ['Monday', 'Thursday']}, {'name': 'Latasha Bryan', 'availability': ['Monday', 'Sunday']}, {'name': 'Crystal Brewer', 'availability': ['Thursday', 'Friday', 'Saturday']}, {'name': 'James Barnes Jr.', 'availability': ['Tuesday', 'Wednesday', 'Thursday', 'Sunday']}, {'name': 'Michel Trujillo', 'availability': ['Monday', 'Tuesday', 'Wednesday']}, {'name': 'Kimberly Warner', 'availability': ['Monday', 'Tuesday', 'Friday']}]
available_frequency = {'Monday': 0, 'Tuesday': 0, 'Wednesday': 0, 'Thursday': 0, 'Friday': 0, 'Saturday': 0, 'Sunday': 0}
def calculate_availability(gamers_list, available_frequency):
for gamer in gamers:
for key,value in gamer.items():
for day in gamer['availability']:
available_frequency[day] += 1
return available_frequency
EDIT: Working further in the project I discovered I had a missed the step creating a function to create a weekday frequency table. If I had gone back through the instructions I would have caught that.
Hi there,
I’m kinda new to this so bear with me.
When checking dictionary keys, the solution does:
But I did:
Am I correct in thinking that the solution checks if there is a value to the keys, but it doesn’t matter what the value is. And mine instead checks if the key is there, no matter if there is an actual value defined for the key? (I assumed that a key completely without a value doesn’t work in a dictionary anyway).
They’re quite similar, you can check the details of how the dictionary .get
method works if you like-
https://docs.python.org/3/library/stdtypes.html#dict.get
However, since .get
returns the value if the key is found it actually does matter what the value is. In an if
statement you would then be testing the truthiness of the value which is very different to just checking if the key exists.
Whilst normally I personally much prefer the straightforward usage of the in
operator that you used (for both readability as it seems more like English and robustness since it only checks for the key leaving no trouble with falsey values), in this example you could have an “availability” key for a gamer which is an empty list. For example:
kimberly = {
"name": "Kimberly Warner",
"availability": []
}
print(bool(kimberly.get("availability")))
Out: False
# an empty list evaluates to False when used as a boolean
print(bool("availability" in kimberly))
Out: True
If you only check for the key then in theory you may add a gamer
to your gamer_list
who actually has no available days which seems like a mistake (say a regular gamer is busy for a week or two but you don’t want to remove all their details, just their availability).
There are of course other ways of dealing with either of those options but be aware that dict.get
is not the same as x in dict
.
By the way you can check the keys without explicitly calling the .keys()
method with just if 'name' in gamer
for example.
This exercise is very badly worded and much harder than any of the topics covered so far. Please work on the wording of these type of projects as they are very off puting for people who are following the course.
For example:
“Write a function called calculate_availability
that takes a list of gamers as an argument gamers_list
and a frequency table available_frequency
. The function should iterate through each gamer in gamers_list
and iterate through each day in the gamer’s availability. For each day in the gamer’s availability, add one to that date on the frequency table.”
What is even a frequency table? This has not been brought up before. How come the exercise suggests to use a variable ‘available_frequency’ as a parameter even though in none of the previous steps we created such a variable.
Another example:
“Lastly we need a way to pick the day with the most available people to attend so that we can schedule game night on that night.”
and then
“Now let’s use these tools to find the best night to run Abruptly Goblins!”
This is the same thing asked of us twice!
I have finished this project without using any of the topics covered in the dictionary section of the course because of how badly this topic was covered and made no sense.
The dictionary and format section of the course was very vaguely covered and then this project comes up that is impossible to figure out. With each off-platform project suggested during the course I am more and more inclined to switch from codeacademy to another platfrom due to how unaccesible the solution is for a beginer.
Hi all.
I used a mix of OP’s post and the given solution when solving this exercise (mainly because in some parts OP’s code is more elegant), but I got stuck in the last part as I have done a previous part a little differently and I don’t seem to comprehend how these are connected. Both OP and the official solution gets this right, I just want to know how and why is that the method used.
so, in the part where we find the best night, I just wrote:
def find_best_night(availability_table):
best_night = max(count_availability, key=count_availability.get)
return best_night
instead of :
def find_best_night(availability_table):
best_availability = 0
for day, availability in availability_table.items():
if availability > best_availability:
best_night = day
best_availability = availability
return best_night
now what happens is that when I run the penultimate part:
unable_to_attend_best_night = [gamer for gamer in gamers if game_night not in gamer['availability']]
second_night_availability = build_daily_frequency_table()
calculate_availability(unable_to_attend_best_night, second_night_availability)
second_night = find_best_night(second_night_availability)
print(second_night)
this gives me Thursday again, and not Monday which should be the solution.
I reckon my solution is a bit too hardcodey and situational so if someone could explain why it’s wrong and why the official solution is the correct approach, I would greatly appreciate it!
I assume that you have something like the following at the beginning of the project
gamers = [
{'name': 'Thomas Nelson', 'availability': ['Tuesday', 'Thursday', 'Saturday']},
{'name': 'Joyce Sellers', 'availability': ['Monday', 'Wednesday', 'Friday', 'Saturday']},
{'name': 'Michelle Reyes', 'availability': ['Wednesday', 'Thursday', 'Sunday']},
{'name': 'Stephen Adams', 'availability': ['Thursday', 'Saturday']},
{'name': 'Joanne Lynn', 'availability': ['Monday', 'Thursday']},
{'name': 'Latasha Bryan', 'availability': ['Monday', 'Sunday']},
{'name': 'Crystal Brewer', 'availability': ['Thursday', 'Friday', 'Saturday']},
{'name': 'James Barnes Jr.', 'availability': ['Tuesday', 'Wednesday', 'Thursday', 'Sunday']},
{'name': 'Michel Trujillo', 'availability': ['Monday', 'Tuesday', 'Wednesday']},
{'name': 'Kimberly Warner', 'availability': ['Monday', 'Tuesday', 'Friday']}
]
As suggested by @spacedude86 in a previous post, you need the available_frequency
dictionary to exist (and have the appropriate keys) before you can add to that dictionary.
You can do that by having something like this to set up the dictionary before the loop (or before the function).
available_frequency = {
'Monday': 0,
'Tuesday': 0,
'Wednesday': 0,
'Thursday': 0,
'Friday': 0,
'Saturday': 0,
'Sunday': 0
}
But, if you have something like "Monday,Tuesday,Wednesday"
as a string that is one of the keys in the gamers
dictionary, then you’ll have to use something like day.split(",")
to get those days separately.
alternative
Alternatively, you could start with an empty dictionary, and create the appropriate key if it does not exist yet.
def calculate availability(gamers_list, available_frequency):
available_frequency = {} #empty dictionary, so that dictionary can be returned
for gamer in gamers_list:
for day in gamer["availability"]:
if day in available_frequency: # check whether the key exists
available_frequency[day] += 1 # if key exists, add 1
else:
available_frequency[day] = 1 # if key does not exist yet, create it
return available_frequency
Isn’t there something wrong with how the task is explained at step [15]?
- Create a list
unable_to_attend_best_night
of everyone ingamers
that wasn’t able to attend game night ongame_night
.
(the important thing here is to remember the “Create a list…” part)
and two steps below it says to:
Call
calculate_availability
withunable_to_attend_best_night
andsecond_night_availability
The problem being that, the function 'calculate_availablity'
created much earlier in step [6] was meant to iterate over the gamers
list in which each element is a dictionary representing each gamer.
While the instructions in step [15] are now asking to use that same 'calculate_availablity'
function but use it instead over a simple list containing only strings of the gamer’s names (what I mentioned at the very top) and not a list containing dictionaries so it therefore doesn’t work anymore!
Anybody else ran into this problem? Shouldn’t this be addressed?