Lab 6: Early Returns and Tracking Variables

For this part of the lab, create a new file called extrapractice.py

Early Returns

Problem 1

Define a function named firstBig below which takes a string as its only argument. It should return the first letter in the list which is in upper-case. The catch is that you must use return inside of the loop that you use (and to get things working properly, this means that you'll need a conditional as well). firstBig should return None if there are no upper-case letters in the string.

Note: if you have a letter in a variable let and call let.isupper() you will get True if the letter is uppercase and False otherwise.

Problem 2

Now write code (NOT a function) which calls your firstBig function and stores the result in a variable named big. Your code should be written so that the value of big will be 'A'. You may not use any variables other than big (which you will only define, not use).

Tracking Variables

Note that it's possible within a value loop to use a tracking variable to remember values from one iteration of the loop to the next. For example, in the loop below, prev is a tracking variable that allows for keeping track of the difference between two successive numbers.

prev = 0
for num in [1, 2, 4, 5, 7, 10]:
    print(num - prev)
    prev = num

Tracking variables include accumulators, but also variables that are used for other purposes to track values during the operation of a loop.

Problem 3

Without running that code, write a series of print statements which prints the same thing that it would print, without using any loops or variables.

Problem 4

Define a function named printDoubles which prints out all of the double-letter pairs in a word. For example:

>>> printDoubles('loop')
oo
>>> printDoubles('Massachusetts')
ss
tt
>>> printDoubles('Mississippi')
ss
ss
pp
>>> printDoubles('AAAH')
AA
AA

As shown above, runs of a letter are treated as overlapping double-letter pairs (you shouldn't need to do anything special to handle this).

Use a for loop and do NOT use range. You may NOT use indexing at all for this problem.

Problem 5

Tracking variables can be used in conjunction with an early return to write loops with complex stopping conditions. Write a function called abbr which takes a word as its only parameter and returns a string which acts as an abbreviation for that word. The abbreviation should contain the first letter of the word no matter what, plus the next up-to-three non-vowel letters in the word (using the provided isVowel function). For example:

>>> abbr('abbreviation')
'abbr'
>>> abbr('YES')
'YS'
>>> abbr('goodbye')
'gdby'
>>> abbr('ambidextrous')
'ambd'
>>> abbr('a')
'a'
>>> abbr('achoo')
'ach'

You may assume that the word has at least one letter in it; you must use a for loop and you may not use range.

Hint: You may want to have two return statements: an early return in the loop and a default return after the loop in case the early return doesn't happen.

You should copy the code below into Thonny as your code for this problem will rely on it.

def isVowel(letter):
    """
    Returns True if the letter is a 1-letter string and it's
    one of the vowels AEIOU (ignoring case).
    """
    return len(letter) == 1 and letter.lower() in 'aeoiu'

Table of Contents