Chapter 4 Exercise Set 0: Chapter Review

We suggest you create a single file containing the test scaffolding from our previous chapter, and put all functions that require tests into that file.

Fix Quack and Ouack

Modify:

prefixes = "JKLMNOPQ"
suffix = "ack"

for letter in prefixes:
    print(letter + suffix)

so that Ouack and Quack are spelled correctly.

Remove a letter

Write a program named remove_a_letter.py that removes all occurrences of a given letter from a string.

A sample run of the program should look something like this:

Enter a string: The rain in Spain falls mainly in the plain.
Enter a letter to remove: i

The ran n Span falls manly n the plan.

Counting the number of a’s in a sentence

Write a program count_a.py that counts the number of times the letter a appears in a sentence.

A sample run of the program should look something like this:

Enter a sentence: The rain in Spain falls mainly in the plain.

The letter "a" appears in your sentence 5 times.

If there are no a’s in the sentence, it should simply say that:

Enter a sentence: There is nothing here.

The letter "a" does not appear in your sentence.

Finally, if there is only a single a the word, time, should be singular:

Enter a sentence: Only happens once here.

The letter "a" appears in your sentence 1 time.

Modulus operator

Evaluate the following numerical expressions in your head, then use the Python interpreter to check your results:

  1. >>> 5 % 2

  2. >>> 9 % 5

  3. >>> 15 % 12

  4. >>> 12 % 15

  5. >>> 6 % 6

  6. >>> 0 % 7

  7. >>> 7 % 0

What happened with the last example? Why? If you were able to correctly anticipate the computer’s response in all but the last one, it is time to move on. If not, take time now to make up examples of your own. Explore the modulus operator until you are confident you understand how it works.

Counting the length of sequences by traversal

Write a loop that traverses:

['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]

and prints the length of each element. What happens if you send an integer to len? Change 1 to 'one' and run your solution again.

Compute the sum

"""Compute the sum of 1 + 2 + 3 + ... + n, and print the total."""
n = int(input("Enter the number to which you want to sum: "))

running_total = 0
num = 1
while num <= n:
    running_total = running_total + num
    num = num + 1

print("The total is: ", running_total)

You can almost read the while statement as if it were English. It means, While num is less than or equal to n, continue executing the body of the loop. Within the body, add num to your running_total, then increment num. When num passes n, leave the loop and print your accumulated sum.

On paper, complete a trace of this program when n is assigned 6.

Counting digits

The following function counts the number of decimal digits in a positive integer:

n = int(input("Enter n: "))

count = 0
while n != 0:
    count = count + 1
    n = n // 10

print(count)

Entering 710 for n will display 3. Trace the execution of this function call to convince yourself that it works.

This function demonstrates an important pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time the loop body is executed. When the loop exits, count contains the result — the total number of times the loop body was executed, which is the same as the number of digits.

If we wanted to only count digits that are either 0 or 5, adding a conditional before incrementing the counter will do the trick:

n = int(input("Enter n: "))

count = 0
while n > 0:
    digit = n % 10
    if digit == 0 or digit == 5:
        count = count + 1
    n = n // 10

print(count)

Confirm that you get 7 when you enter 1055030250 for n.

Notice, however, that entering 0 for n will not give you 1. Explain why. Do you think this is a bug in the code?

Invitations to a party

Write a program named lets_party.py that asks the user to enter a list of names and then prints out messages to each one of them inviting them to a party.

A sample run might go something like this:

Enter invitee's name (or just enter to finish): Alejandro
Enter invitee's name (or just enter to finish): Edward
Enter invitee's name (or just enter to finish): Harrison
Enter invitee's name (or just enter to finish): Miguel
Enter invitee's name (or just enter to finish): Margot
Enter invitee's name (or just enter to finish): Kathryn
Enter invitee's name (or just enter to finish): Elliot
Enter invitee's name (or just enter to finish): Justin
Enter invitee's name (or just enter to finish): Prisila
Enter invitee's name (or just enter to finish): John
Enter invitee's name (or just enter to finish): Sean
Enter invitee's name (or just enter to finish): George
Enter invitee's name (or just enter to finish): Peter
Enter invitee's name (or just enter to finish): Jonathan
Enter invitee's name (or just enter to finish):

Alejandro, please attend our party this Saturday!
Edward, please attend our party this Saturday!
Harrison, please attend our party this Saturday!
Miguel, please attend our party this Saturday!
Margot, please attend our party this Saturday!
Kathryn, please attend our party this Saturday!
Elliot, please attend our party this Saturday!
Justin, please attend our party this Saturday!
Prisila, please attend our party this Saturday!
John, please attend our party this Saturday!
Sean, please attend our party this Saturday!
George, please attend our party this Saturday!
Peter, please attend our party this Saturday!
Jonathan, please attend our party this Saturday!

Divisible by 2 or 3

Write a program that prints out each number from 2 to 20 and writes whether the number is divisible by 2 or 3, both, or neither. The output should look like this:

Num   Div by 2 and/or 3?
---   ------------------
2            by 2
3            by 3
4            by 2
5            neither
6            both
7            neither
8            by 2
9            by 3
10           by 2
11           neither
12           both
13           neither
14           by 2
15           by 3
16           by 2
17           neither
18           both
19           neither
20           by 2

First multiple of 7

Write a program that traverses a list of numbers and prints out the first number in the sequence that is a multiple of 7 (hint: you may want to use a break statement for this) for the message, “No multiples of 7 found.” if none of the values in the list are multiples of 7.