Skip to content

Practice Quiz 3 (Ch 5–6)

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: ____________

Written Portion

Question 1  (4 pts)

Evaluate each expression below. If an error occurs, write the word Error. Use double quotes for string values.

sports = {"football", "basketball", "track", "tennis", "swimming"}
players = ["Jeff", "Claire", "Billy", "Georgina", "Jeff"]
players_set = set(players)
teams = {"Ducklings": 12, "Crows": 3, "Starlings": 18}
Expression Result Expression Result
teams["Starlings"] 18 players[2][-1] "y"
len(teams) 3 players[teams["Crows"]] "Georgina"
sports[2] Error "track" in sports "True"
len(players_set) 4 teams[1] Error

Question 2  (4 pts)

Given the following variables:

people = {"William", "Jessica", "Mona"}
residents = {"horse": 5, "pony": 1, "sheep": 4, "chicken": 5}
animal = "cow"
count = 6
person = "Dylan"

Write a statement (one line of code) for each description.

a. Create a tuple containing the values of the variables person, animal, and count and assign it to a variable named team.

Answer:   team = (person, animal, count)

b. Add the value of person to the set people.

Answer:   people.add(person)

c. Print, on the screen, the value of a boolean expression that tests if the value of animal is in the dictionary residents.

Answer:   print(animal in residents)

d. Write a statement to insert the key in the variable animal and the value in the variable count into the residents dictionary.

Answer:   residents[animal] = count

Question 3  (2 pts)

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


sci_fi = ("Interstellar", "Coherence", "Blade Runner")

a = 0
z = len(sci_fi) - 1
while a <= z:
    print(sci_fi[a])
    a += 3
print(a)

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

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)