Create a subfolder called lab7 within your
cs111 folder, and put all of the files for this lab in that
folder.
In Part II of Problem Set 5, you will write a number of functions that use a loop to process a sequence. This task of the lab helps to prepare you for those problems.
In Spyder, open a new editor window and save it to your lab7 folder
using the name lab7.py.
Earlier in the course, we covered a recursive function called
num_vowels that took an arbitrary string of lower-case letters
as input and returned the number of vowels in the string. For
example:
>>> num_vowels('hello') result: 2 >>> num_vowels('aloha') result: 3
Write a version of this function that uses a loop instead of recursion. Here’s a template:
def num_vowels(s): """ returns the number of vowels in a string s consisting of lower-case letters """ count = 0 for c in s: # complete the rest of the function!
Note that this function should perform a cumulative computation that gradually builds up the final count of the number of vowels.
We also covered a recursive function called rem_all that took an
arbitrary list of values vals and a single element elem and
that used recursion to create a new list in which all occurrences of
elem were removed. For example:
>>> rem_all(10, [3, 5, 10, 7, 10]) result: [3, 5, 7] >>> rem_all(4, [2, 4, 8, 4, 4]) result: [2, 8]
Write a version of this function that uses a loop instead of recursion. Here’s a template:
def rem_all(elem, values): """ returns a list in which all occurrences of elem in values have been removed """ result = [] for ___ in ____: # complete the rest of the function!
Note that this function also needs to perform a cumulative
computation! We have called the accumulator variable result,
and we’ve initialized it to be an empty list. Inside the loop,
you should use list concatenation to gradually build up a
list containing all of the values that belong in
the final result.
Important: Although an element-based loop would work here, you should use an index-based loop to get practice with that type of loop. If you need help starting that type of loop, feel free to ask a staff member.
In lecture, we learned how to use the input() function to get
a value from the user. Write a function get_pos_int(prompt)
that takes a string prompt and performs the following tasks:
gets an initial integer value from the user using the input
function and the specified prompt
if the user does not enter a positive integer, keeps asking for additional inputs until the user enters a positive value; these additional inputs (if any) should use the following prompt:
The input must be positive. Try again:
once the user enters a positive value, returns the value.
You may assume that the user always enters an integer.
For example:
>>> get_pos_int('Enter your age: ') Enter your age: -2 The input must be positive. Try again: -5 The input must be positive. Try again: 0 The input must be positive. Try again: 20 result: 20
Hints:
This problem lends itself to an indefinite loop (i.e., a
while loop). Why?
Don’t forget that input always returns a string. As a result,
you will need to use the int function to convert the input
to an integer as discussed in lecture.
As time permits, the course staff will be giving you a chance to do some review for midterm 1.
Important: You will not be allowed to use loops on this midterm exam!
Complete any of the problems listed under the Warmup-2 collection of problems on CodingBat. Rather than solving them using recursion, use a loop instead!
Last updated on October 20, 2025.