The main topics on this worksheets are lists, while loops and for loops.
Collaboration on worksheets is strongly encouraged.
The main course materials to refer to for this worksheet are:
(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)
An arithmetic progression is a sequence of numbers in which the difference between any pair of neighboring terms is the same, e.g. $$ 1, 5, 9, 13, 167, 21, ... $$ where the difference is 4, or $$ -58, -42, -26, -10, 6, 22, ... $$ where the difference is 16.
An arithmetic progression is determined by its first element $a_0$ and the difference $\delta$ between neighboring terms. The term at index $i$ then has the formula $a_i = a_0 + \delta * i$.
Write a program that allows the user to specify an arithmetic progression and a desired number of terms, and which then prints the terms, one per line, as shown below:
First term: -100
Difference of neighboring terms: 7
Number of terms: 13
The first 13 terms of that arithmetic progression are:
-100
-93
-86
-79
-72
-65
-58
-51
-44
-37
-30
-23
-16
In this example, -100
, 7
, and 13
on the first three lines are numbers entered by the user. Note that means your program will have three input()
statements to read these data.
Hints:
Write a script that lets the user enter any number of words, one per line. The script should store all of the words the user enters in a list. When the user is finished, they simply press Enter instead of entering a word, and the script takes this as a signal to stop waiting for additional input. It then prints a report giving:
Here's a sample session which shows what the prompt and output should look like:
Enter any number of words, one per line, or a blank line to stop:
we
are
closely
monitoring
the
situation
and
will
announce
something
shortly
Number of words: 11
First word: we
Last word: shortly
Middle word: situation
Total number of letters: 65
Hints:
while
loop to repeatedly read words until a blank one is found (with input(...)
in the body of the loop).while
statement need to exist before the loop begins, and you won't have read the first word when the loop starts.You are presented with a wooden box. Across the top, it has a row of five light bulbs. (To be able to refer to the lights individually, let's call them 0, 1, 2, 3, and 4.) Lights 1 and 2 are turned on when you first receive the box. Below the lights is a label that reads "Puzzle Box - Can you turn on all of the lights?" There are also two large circular buttons, labeled "a" and "b".
You discover that pressing button "a" makes the state of the lights shift one position to the right, with the rightmost light's state wrapping around to the left side. In other words, after pressing button "a", light $n$ will be in whatever state light $n-1$ was in before you pressed it.
You also discover that pressing button "b" makes lights 0, 2, and 4 switch state (from on to off or off to on, whichever is appropriate for the light).
Before trying out the physical puzzle, you decide it would be best to experiment on your computer.
Write a Python script that simulates the puzzle box, displaying the state of the lights using 0 and 1 to represent off and on, respectively. The script should keep showing the state of the lights and waiting for a button press until the user wins (i.e. until the state is 11111
.) Here's what the interface should look like (and a sample of how you might win):
01100
Which button? a
00110
Which button? b
10011
Which button? b
00110
Which button? a
00011
Which button? b
10110
Which button? b
00011
Which button? a
10001
Which button? b
00100
Which button? a
00010
Which button? a
00001
Which button? b
10100
Which button? a
01010
Which button? b
11111 you win!
Hints:
input(...)
and then decide which of two things you need to do to modify the light state list. What Python construct is best for that?print(x,end="")
. For example, compare the two programs shown below.for x in range(3):
print(x)
for x in range(3):
print(x,end="")
Work on this if you have extra time. Solutions won't be provided for this part.