Homework #7


The goal of this assignment is to gain experience working with:

  • while loops:
    • user input with sentinel values
    • until reaching a numerical threshold
    • iterating over a list


For this homework, and all of the remaining homeworks, all of your modules and functions must exhibit correct documentation. Submissions that don’t pass docstring checks will receive no credit.

You should read the Docstring Requirements carefully to see how it should be done.

Exercise 7.1 While Signs

Back in HW 6 you created a program that determined if numbers were positive or negative, and another that sorted numbers into even and odd. Now you will create a function that sorts numbers input by the user into separate lists of positive and negative numbers. Your program will read numbers from the keyboard until the user inputs a ‘q’.

Name your program while_signs.py. Write a function get_positive_negative() that:

  1. Reads keyboard entries until the user inputs a ‘q’. The prompt should be Input? (notice the trailing space).
  2. Sorts each float input into the appropriate list: positive numbers or negative numbers
  3. Returns the two lists as a tuple, positives first.

For instance:

>>> %Run while_signs.py
>>> result = get_positive_negative()
Input? 34
Input? 4
Input? -7
Input? 0
Input? q
>>> print(result)
([34.0, 4.0], [-7.0])

Exercise 7.2 Division Algorithm

Computers aren’t born knowing how to perform integer division; they must be programmed to do it. One such method is the division algorithm, described below.

Say you want to divide an integer n ≥ 0 by an integer d > 0. To find the quotient and remainder, follow these steps:

  1. Initialize the variable q with the value 0.
  2. Initialize the variable r with the value n.
  3. As long as r ≥ d, repeat the following:
    • Add 1 to q.
    • Subtract d from r.
  4. Then q will be the quotient and r will be the remainder.

You can test you’ve done it right by checking that n = qd + r.

In a file named division.py, write a function divide that takes two parameters, number and divisor, in that order, that performs the division algorithm to find number/divisor and returns the two values q and r in that order.

Exercise 7.3 Even More Basketball Stats

Back in Exercise 3.3 you created a program for JMU Basketball that printed the scoring for a single player, and in Exercise 5.4 a function that returned all the statistics for a single player. They would now like you to create a function that will print the statistics for a large number of players all at once.

Name your program even_more_stats.py. Write a function called print_stats that does two things:

  1. it prints the names and individual statistics for any number of players; and
  2. it prints the total number of points, rebounds, and assists for the entire group of players.

The function will take a single parameter, a list that contains all the statistics for the players. A sample list is as follows:

[('Jefferson', 706, 88, 57), ('Hazell', 615, 62, 62), ('Tucker', 551, 137, 17)]

(Statistics from 2020-2021 JMU Women’s Basketball Team)

The list consists of tuples, each of which lists a player’s name, their total points, total rebounds, and total assists for the season. Your code should print a line for each player (as demonstrated below), and then print the total points, total rebounds, and total assists, calculated for the entire list.

The output should be as follows:

>>> print_stats([('Bird', 500, 100, 50), ('Jordan', 450, 90, 45)])
Bird scored 500 points, grabbed 100 rebounds, and made 50 assists.
Jordan scored 450 points, grabbed 90 rebounds, and made 45 assists.
Total Points: 950
Total Rebounds: 190
Total Assists: 95

Note: Iterating through a list of elements like this would normally be accomplished using a for-loop, nota while-loop. We’ll see how to use for-loops for problems like this next week. For now, you solution must use a while-loop.

Back to Top