Skip to content

Practice Quiz 4 (Ch 7–8)

Name: ___________________________________

This work complies with the JMU Honor Code. I have neither given nor received unauthorized assistance. I will not discuss the quiz contents with anyone who has not taken the quiz.
Initials: ____________

Question 1  (3 pts)

my_dog = "Pongo"
all_dogs = ["Pongo", "Nina", "Duke"]

def feed_dog(dog_to_feed):
    message = "appreciates the meal!"
    print(dog_to_feed, message)

feed_dog(my_dog)
print(len(all_dogs))

Determine the namespace (built-in, global, or local) of each item.

Item Namespace Item Namespace
my_dog global message local
print built-in dog_to_feed local
feed_dog global len built-in

Question 2  (4 pts)

For each code snippet, determine: (1) how many times the loop runs; (2) what is printed on the screen.


flavors = ["vanilla", "chocolate", "mint", "strawberry", "banana"]

i = 4
while i >= 0:
    print(flavors[i], end=" ")
    i -= 2
print(i)

How many times the loop runs:   3                   What is printed on the screen:   banana mint vanilla -2


i = 0

while i < 20:
    i += 1

    if i % 2 == 0:
        continue
    elif i == 5:
        break
    else:
        print(i, end=" ")

How many times the loop runs:   5                   What is printed on the screen:   1 3

Question 3  (3 pts)

money.py

def get_change(price, paid):
    return paid - price

print("Money", end=" ")

if __name__ == "__main__":
    print(get_change(3, 4), end=" ")

restaurant.py

import money

BURGER_PRICE = 3

def process_payment(customer, paid):
    change = money.get_change(BURGER_PRICE, paid)
    print(customer, end=" ")
    print(change, end=" ")

process_payment("Ronald", 5)


a. What will be printed when money.py is run?   Money 1


b. What will be printed when restaurant.py is run?   Money Ronald 2

Question 4  (7 pts)

Implement the following function. Please write clearly and indent your code using four spaces.

def count_woofs(noises):
    """Return the number of times "woof" appears in a list.

    You MUST use a while loop! Use of count() is NOT allowed.

    Example: count_woofs(["woof", "meow", "woof"]) returns 2, because
    "woof" appears two times in the list.

    Args:
        noises (list): a list of strings.

    Returns:
        int: number of times "woof" appeared.
    """
    i = 0
    count = 0
    while i < len(noises):
        if noises[i] == "woof":
            count += 1
        i += 1
    return count

Question 5  (8 pts)

Implement the following function. Please write clearly and indent your code using four spaces.

def get_numbers(limit):
    """Get a list of numbers from the user that are less than the limit.

    Repeatedly prompts the user to input numbers (int) into a list until
    the user enters a number that is greater than or equal to the limit.

    In the following example, get_numbers(5) is called. The user inputs
    the numbers 1, 3, and 5. The function then returns the list [1, 3].

    >>> get_numbers(5)
    Enter a number: 1
    Enter a number: 3
    Enter a number: 5
    [1, 3]

    Args:
        limit (int): The limit for determining when to stop.

    Returns:
        list: The numbers entered (except for the last one).
    """
    numbers = []
    while True:
        value = int(input("Enter a number: "))
        if value >= limit:
            return numbers
        numbers.append(value)