Create a subfolder called lab8
within your
cs111
folder, and put all of the files for this lab in that
folder.
Trace the following program using a table, and determine the output:
for i in [3, 2, 1]: for j in range(i): print(i * j) print(j)
Here’s the beginning of the table:
i |
range(i) |
j |
value printed |
---|---|---|---|
3 |
[0, 1, 2] |
0 |
|
... |
Because the sequence used by the inner loop (range(i)
) depends on
the value of the outer loop’s variable (i
), we’ve included a column
in the table for that sequence.
As discussed in lecture, list are mutable, which means that we can modify their internals using assignment statements that look like this:
values[i] = expression
This type of operation is referred to as item assignment,
and it changes the value of the item stored at position
i
in the list values
.
To see how this works, try the following in the IPython console:
>>> values = [2, 4, 6, 8] >>> values[1] = 0 >>> values[3] = 1 >>> values
What do you see? Does it make sense? Ask a staff member if it doesn’t!
Consider the following Python program:
def foo(values, x): values[1] = 2 * values[1] x += 1 a = [4, 5, 6] b = a[:] c = a x = 5 foo(c, x) print('a =', a) print('b =', b) print('c =', c) print('x =', x)
Let’s trace through it together in Python Tutor.
Before the function call foo(c, x)
is made:
How many distinct lists are there?
Which variables refer to the same list?
During the execution of the function:
What changes does the function make?
When the function call is about to complete, what return value does Python Tutor specify? Why does this make sense?
After the function call returns:
What output is produced?
Why have the values of some of the variables changed, while others have not?
The manager of the BU Terriers hockey team has asked us to write an interactive program that will allow them to analyze the team’s results.
These results will be represented by two lists of integers:
one list containing the goals scored by the Terriers in each of a series of games
one list containing the goals scored by their opponents in those same games.
For example, here is one possible set of results:
terrier_goals = [1, 4, 3] opponent_goals = [2, 1, 3]
Looking at these lists, we see that:
The Terriers lost the first game 1-2, because there is a 1 in position 0 of their list of goals, and a 2 in position 0 of their opponent’s list of goals.
The Terriers won the second game 4-1, because there is a 4 in position 1 of their list of goals, and a 1 in position 1 of their opponent’s list of goals.
The Terriers tied the third game 3-3, because there is a 3 in position 2 of both lists of goals.
In lab8.py
, we’ve given you
some starter code for this program. Download that file now, saving
it in your lab8
folder, and open the file in Spyder.
Reviewing the starter code
The starter code includes several functions:
one called display_menu()
that prints a menu of options to the user
one called latest_score()
that takes two lists of results and
returns the score of the latest (i.e., most recent) game
one called main()
that is used to start the program, and to
repeatedly carry out tasks selected by the user.
Let’s start by reviewing the main()
method together. Among other things,
we should answer the following questions:
What is the purpose of the while
loop in main()
?
What is the purpose of the following line of code?
choice = int(input('Enter your choice: '))
What is the purpose of the if-elif-else
statement in main()
?
What happens when the user enters a value of 0 for choice
?
What happens when the user enters a value of 1 for choice
?
When does the while
loop end?
Running the program
To run the program, you should:
Run lab8.py
in Spyder.
Call the main()
function from the console:
>>> main()
At the moment, the only supported menu choices are 0 and 1, so go ahead and try executing each of those choices.
Now let’s implement additional functionality that our program will need.
Option 2 in the menu is to find the max number of goals in a list of
goals. In the lab8.py
file, we’ve provided the following function
for this purpose:
def find_max_goals(goals): """ returns the largest number of goals in the specified list of goals, which we assume contains at least one integer """ maxg = goals[0] for g in goals: if g > maxg: maxg = g return maxg
For example:
>>> find_max_goals([3, 2, 5, 1]) 5 >>> find_max_goals([3, 1, 2, 0]) 3
Let’s integrate this function into the interactive program.
In the main()
function, add a new elif
block that can be used
when the user enters a 2. Make sure that your new elif
block
goes in between the existing elif
blocks and the else
block.
Within the new elif
block, you should:
Make an appropriate function call to your new function, and assign its return value to a variable.
Use that variable to print an appropriate message to the user.
See our implementation of choice 1 for an example of the type of code that you should add.
A sample run that illustrates what the behavior of the program should look like for each menu option is available here.
Now we’ll add support for menu option 3: Print the results.
Write a function print_results(terriers, opponents)
that takes
two lists of integers containing the goals scored by the
Terriers and their opponents (see above), and that prints the
results of each game in the format shown below. The function should
not return a value.
Put it in the designated area – in between the existing
find_max_goals()
and main()
functions.
Here are two examples of what the format should look like:
>>> print_results([1, 4, 3], [2, 1, 3]) loss 1-2 win 4-1 tie 3-3 >>> print_results([5, 3, 2, 1], [2, 1, 2, 4]) win 5-2 win 3-1 tie 2-2 loss 1-4
Before you begin coding, ask yourself which type of for
loop makes
the most sense here: element-based or index-based?
Hint: To print the score for a given game, you’ll need to:
use string concatenation so that there will be no spaces between
the numbers and the hyphen (-
)
use the str()
function to turn each team’s number of goals into
a string, so that you can then concatenate it.
Integrate this new function into the interactive program by taking similar steps to the ones that you took in step 2 above.
Because this function doesn’t return a value, you can simply
call the function from within a new elif
block in main()
, and
you won’t need to do anything after it returns.
Once again, you should consult the sample run as needed.
The team would also like your program to be able to compute their complete record – i.e., their number of wins, losses, and ties.
This new functionality will be menu choice 4.
Write an appropriate separate function for choice 4. Your function
should take whatever inputs it needs, and it should return the record
as a list of the form [wins, losses, ties]
.
Once your function is working, add a new elif
block for it in
main()
, and update the display_menu()
function to add the choice
to the menu.
On a sheet of paper, trace the following program using a table, and determine the output:
values = [7, 2, 5, 3, 8] for i in range(len(values)): if values[i] % 2 == 1: values[i] = -1 * values[i] print(values)
Here’s the beginning of the table:
i |
values[i] |
action taken (if any) |
---|---|---|
0 |
7 |
values[0] = -1*7 = -7 |
... | ||
What is the output of this program?
Rewrite the program from part 1 so that it uses a while
loop
instead of a for
loop. The revised program should process the
list in the same way as the original program. Here’s a template you
can use:
values = [7, 2, 5, 3, 8] i = 0 while __________________: if values[i] % 2 == 1: values[i] = -1 * values[i] ______________________ print(values)
Last updated on October 28, 2024.