It was another tough week for me on KA. 😔 I only made it through one Lesson in Unit 6 in the Intro to Computer Science – Python course. I did, however, continue making decent progress outside of KA. I worked through a handful of videos from a Python playlist on YouTube from a creator named Dave Gray, and I made a bit of progress working through the book Automate the Boring Stuff with Python. I did a bunch of exercises from both the playlist and the book using the code editor VS Code, so I’m not only making progress understanding how to write Python syntax, in general, but also using the VS Code editor (which, apparently, is one of or the most widely used code editors by professionals). Nonetheless, I’m definitely disappointed in my effort on KA. I’m guessing I studied for 5 hours and probably spent ~15–20 hours in total studying CS, but I likely could have studied for ~25–30 hours, so I feel like I let myself down. Still, I’m making progress! Just need to keep going strong and get some momentum going again. 💪🏼
Here’s everything I made notes on this week:
Unit 6 – Analyzing Data with Dictionaries
Lesson 1 – Dictionaries
Challenge: Login Portal


I started this week by getting back to this challenge. I’d finished at the end of last week, except there was a ‘bonus’ part to it (which you can see in the left panel of the screenshot) that I wanted to complete. I needed to write code to generate a random 14 character password. I (obviously) got Gemini to help me with this and it helped me write this function:
def generate_password():
“”” This function will generate a password for the user that’s
14 characters long and will have lowercase and uppercase letters,
and digits and punctutation.
“””
all_characters = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
password_items = []
password_items.append(random.choice(string.ascii_lowercase))
password_items.append(random.choice(string.ascii_uppercase))
password_items.append(random.choice(string.digits))
password_items.append(random.choice(string.punctuation))
for i in range(10):
password_items.append(random.choice(all_characters))
random.shuffle(password_items)
secure_password = ”.join(password_items)
return secure_password
One thing I didn’t know (among many) was that you could import a built-in module called “string” and use string.ascii_lowercase and the 3 other methods shown in the second screenshot above to get a list of characters respective to each method. Although the function above doesn’t look like that much code, it took me ~1 hour to write. 😕 I also had to write some code on main.py to ask the user if they wanted to create their own password or if they wanted one generated for then, which was this:
if login.needs_account():
print(“\n— Create an account —“)
new_username = login.select_new_username(users.accounts)
print()
auto_gen = input(“Would you like us to generate a password for you? (y/n): “)
if auto_gen.lower() in [“yes”, “y”]:
new_password = login.generate_password()
print(f”Your new passwrod is {new_password}\n”)
else:
new_password = login.select_new_password(8)
users.accounts[new_username] = new_password
All in all, I probably spent ~1.5 hours finishing this off.
Lesson 2 – Dictionary Iteration
Article – Dictionary Methods
This article talked about 3 dictionary methods: .get(), .pop(), and .update():


This example shows how the .get() method works. In the first example, settings.get(“size”, 12) looks in the dictionary called settings for the key “size”, see’s that the value is 16, so prints 16. In the second example, since the key it’s looking for (size 123) doesn’t exist, it prints the ‘default value’ which is 12. In the third example, “color” isn’t in the dictionary and no default value is given so it prints None. And the last example shows that “black” was used as the default value which was printed.

This .pop() method pulls an item out of a dictionary and returns the value. You can see in the first example how “italic” gets returned and “style” is removed from the dictionary. The second example returns 1 since “spacing” was not in the dictionary. And the last example shows KeyError in the terminal because “spacing” isn’t in the dictionary and no default value was given.

This example shows how the .update() method works. In the first example, “spacing: 2” gets added to the dictionary and “bold” replaces “italic” in the dictionary. In the second example, “1.5” replaces “2” and “’color’: red” gets added.
Video – Dictionary Iteration


This video talked about how to leep through items in a dictionary, which I believe is referred to as iterating through the dictionary (although that terminology doesn’t make a lot of sense in my mind 🤔). You use the usual ‘for’ loop syntax, but the items are not indexed the way the are in a list. The loop goes through each item in chronological order. In the second screenshot above you can see the different ways to loop through a dictionary. The first way would print each word in the first screenshot. The second way would print each ‘count’ (i.e. the number of times the word showed up) and the third way would “unpack” the items, printing each word and count separately, one after the other in the terminal, going through the items in the dictionary chronologically.
Article – Dictionary Iteration Patterns
This article covered the different ways to — in the article’s words — “iterate over a dictionary”: by key, by value, and by item.

The bad news is that there’s 0% chance I could write this code by myself (right now, anyways), but the good news is I pretty quickly was able to read it and understand what was going on. The dictionary “sightings” is in wildlife.py and contains items like ‘Cobra’: 15. The function lops through the dictionary by key (species) checking to see if “Deer” was in each key and adding the entire key to the similar_species list, if so.

In this example, You can see that the dictionary is iterating through the values where it says sightings.values(). The equation itself (numerator += count * (count – 1)) is confusing to me, but I understand how the function pulls each value through the function and updates the numerator and denominator accordingly.

Again, I was pretty quickly able to read through and understand this function and how it works, although I never would have been able to take a blank IDE and write it myself. I can see how the loop iterates through the dictionary checking to see if each item’s value is less-than the threshold of 5 and, if so, adds that item back to a new dictionary called filtered. (The notation filtered[species] = count that adds the item to the filtered dictionary is confusing to me. 😒)
Exercise – Iterate Over Dictionaries
Question 1


Question 2


This exercise was easy, although I did spend a minute or two on each question just reading everything through carefully to make sure I understood it. (I’m guessing the more I study Python, the quicker I’ll be able to understand exactly what this type of code is saying just at a glance.) But overall, it was a pretty simple exercise and I went 4/4 in about 5 minutes.
Challenge: Crop Rotation


This challenge was pretty hard. The screenshot just above was taken after I completed Step 1. I had to use Gemini’s help and had initially written code that said ‘if crop not in kgs_per_hectare.items()’ but I should have simply had ‘if crop not in kgs_per_hectare’ with no .items(). But after that, Gemini helped me understand that I could just delete the entire if statement and change the entire thing to ‘crop_yield[crop] = hectares * kgs_per_hectare.get(crop, 0)’ where hectares is the value in the farms.iowa dicationry and kgs_per_hectare(crop, 0) is the value in crops.kgs_per_hectare dictionary. And, if the key “crop” isn’t in the dictionary, then it returns 0 as the default value. So, this is what I ended up replacing my if statement with after.

I got through Step 2 on my own, but knew I could basically just reuse the code from Step 1. BUT, I at least understood how it pulled the values from each dictionary.

I was able to think through the logic of how to execute this step, which I wrote out as the three comments you can see in the screenshot above. I asked Gemini is I had the logic figured out properly and Gemini said I had it figured out exactly the right way, which I was pretty happy about. However, I didn’t know how to switch the value from the “rotation” dictionary to be the key for the new dictionary. The code you see written for Steps 1 and 2 was explained/given to me by Gemini. Once I saw the code, it made complete sense to me, so I at least had the going for me which was nice. I got the final part (the comment that says “Step 3”) on my own which was a relief.
And that was it for this past week. I think I’ll likely do the same thing at the start of the coming week as I did this past week and begin by trying to tackle to bonus Step in the challenge above, but I’m a bit concerned that it will be difficult. I’m finding it harder and harder to motivate myself to keep working on these types of coding challenges because they’re not coming easy to me. This is partly why I’ve been trying to diversify where I’m studying CS, so I can hopefully get a better grasp on the basics of coding so that same type of exercises like the challenge above come easier to me. My plan is to just try and grind and pick away at KA, and the other things I’m working on one small step at a time so that, eventually, programming these types of things becomes second nature.
So, as always, fingers crossed I can have a productive week!🤞🏼 Or, at the very least, a more productive one than this past week. 😬