9. Bugz

Studios are in-class activities to give you hands-on practice with new concepts. The first half is the Walkthrough, an instructor-led programming problem. The second half is for you to work on individually or in pairs in class.

These problems are not graded, and you are not obligated to finish them. Get as far as you can while in class, and use them as an opportunity to play with and solidify new concepts.

Walkthrough

This is LaunchScrabble, a unique version of Scrabble! This function takes a list of words from a LaunchScrabble game and calculates the player’s score. All words count as one point, but words that begin with ‘q’ are 10 points!

But since ‘q’ words are worth so many points, we want to make sure our players aren’t cheating and making up words that start with ‘q’. So for every word that begins with ‘q’, the program checks to make sure the following letter is ‘u’. If we find an exception, for instance “qrie”, negate all their points and give that cheater an overall score of 0!

The only problem is, the code we have so far doesn’t work.

Problems:

  1. All words that do not begin with ‘q’ will automatically return 0
  2. q words add 11 to the total points

There are a couple ways to fix this (for instance, you could use nested conditionals or boolean logic to deal with the ‘q’ words), but one possible solution is below.

Studio

Today’s studio, rather than explicitly pertaining to strings, is more of an overall review. You will get some practice debugging a variety of common mistakes that new coders make.

Below are a handful of code snippets that are broken. See if you can fix them!

Printing the ith Element

The function below, print_every(i, nums) receives a list of numbers nums, along with an integer i. The function is supposed to print every ith element from the list. But it’s not working!

Seniors Bar

You are the bouncer outside the door of a seniors only bar. People must be 70 or older, otherwise they are not allowed in. When a group of friends arrives, your job is to determine whether to accept or reject the group. The function below, check_group, is supposed to return a boolean indicating whether or not the group is allowed inside. But it’s not working!

Password Check

When registering for an online account, users must create a password. For your service, you enforce the following rules on passwords: The password must contain at least one non-alphabetical character, and may not contain any spaces. The function below is supposed to check the validity of passwords. But! It’s! Not! Working!

Bonus Missions

  • Write a function called stretch that takes in a string and doubles each of the letters contained in the string. For example, stretch("chihuahua") would return “cchhiihhuuaahhuuaa”.
  • Add an optional parameter to your stretch function that indicates how many times each letter should be duplicated. The default value for this optional parameter should be 2. For example, stretch("chihuahua", 4) would return “cccchhhhiiiihhhhuuuuaaaahhhhuuuuaaaa” but stretch("chihuahua") should still return “cchhiihhuuaahhuuaa”, as before.
  • Add an additional optional parameter to your stretch function that contains a list of characters. This version of stretch will only duplicate letters that are contained in the list. The default value for this new parameter should be the list of lowercase characters. For example, stretch("chihuahua", 3, "ha") would return “chhhihhhuaaahhhuaaa”.

You can read about optional parameters, also called optional arguments, in this Python Tutorial section .