7.14. ExercisesÂ¶

In Robert McCloskeyâ€™s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop tries to output these names in order.
Of course, thatâ€™s not quite right because Ouack and Quack are misspelled. Can you fix it?

Get the user to enter some text and print it out in reverse order.

 Write a program that uses a for loop to print
One of the months of the year is January
One of the months of the year is February
One of the months of the year is March
etc â€¦

Assume you have a list of numbers
12, 10, 32, 3, 66, 17, 42, 99, 20
Write a loop that prints each of the numbers on a new line.
Write a loop that prints each number and its square on a new line.

Write a program that asks the user for the number of sides, the length of the side, the color, and the fill color of a regular polygon. The program should draw the polygon and then fill it in.

A drunk pirate makes a random turn and then takes 100 steps forward, makes another random turn, takes another 100 steps, turns another random amount, etc. A social science student records the angle of each turn before the next 100 steps are taken. Her experimental data is
160, 43, 270, 97, 43, 200, 940, 17, 86
. (Positive angles are counterclockwise.) Use a turtle to draw the path taken by our drunk friend. After the pirate is done walking, print the current heading. Assume that the turtle originally has a heading of 0 and accumulate the changes in heading to print out the final. Your solution should work for any sequence of experimental data.

: Write a program that will go through a list of temperatures and print them out to the user.temperatures = [3, 78, 95, 28, 56, 42, 56, 81, 10, ]  for temp in temperatures:  print("The weather outside is: " + str(temp))

: Write a program that will print out a greeting to each student in the list. This list should also keep track of how many students have been greeted and note that each time a new student has been greeted.students = ["Jay", "Stacy", "Iman", "Trisha", "Ahmed", "Daniel", "Shadae", "Tosin", "Charlotte"]  num_students = 0  for student in students:  print("Welcome to class, " + student)  num_students += 1 print(str(num_students) + "student(s) have entered the classroom")
7.14.1. Contributed ExercisesÂ¶
Q1: Construct a block of code that correctly implements the accumulator pattern.x = 0
======
for i in range(10)
x = x + 1
Write a for loop that prints the odd numbers from 1 to 101.
The \(L^{2}\text{norm}\) is the most common method for calculating the norm or magnitude of a vector. For an \(N\text{dimensional}\) vector, the \(L^{2}\text{norm}\) is calculated as
For example, the norm of the vector \(\vec{v}=\left(3,0,4\right)\) is
Compute and print out the \(L^{2}\text{norm}\) of the vectors defined below.
The wavelengths of the spectral lines from hydrogen are given by the Balmer series
where \(\lambda\) is the wavelength in m and
\(R_\text{H}=1.0974\times10^7~\text{m$^{1}$}\). Using a forloop, compute the
wavelengths of the first five spectral lines and store them in a
list called wavelengths
.
In the code below, a \(3\times3\) matrix has been define for you. Using loops, update this matrix by scalar multiplying by 5.7.
Hint: you will need two nested loops.
The average monthly rainfall in inches recorded at the Van Nuys Airport for
2018 is stored in the rainfall
variable provided below.
Using a loop, calculate and print out the
total,
mean, and
standard deviation,
each on a separate line.
The standard deviation is given by
where \(\bar{x}\) is the mean.
Do not use the sum()
function. You should calculate all three values using two loops.
The average monthly rainfall in inches recorded at the Van Nuys Airport for
2018 is stored in the rainfall_in
variable provided below.
Using a loop, calculate and print out the
total,
mean, and
standard deviation,
in centimeters, each on a separate line.
The standard deviation is given by
where \(\bar{x}\) is the mean.
Do not use the sum()
function.
In Robert McCloskey’s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop tries to output these names in order.
Of course, that’s not quite right because Ouack and Quack are misspelled. Can you fix it?
Get the user to enter some text, save it to a variable called user_input
, and print it out in reverse order.
Get the user to enter some text, save it to a variable called
user_input
, and print out True if it’s a palindrome, False
otherwise. (Hint: reuse some of your code from the last
question. The == operator compares two values to see if they are
the same)
Assume you have a list of numbers 12, 10, 32, 3, 66, 17, 42, 99, 20
Write a loop that prints each of the numbers on a new line.
Write a loop that prints each number and its square on a new line.
This code is supposed to multiply all the elements in the list
lst
. However, the code is wrong and doesn’t produce the right
result. Fix all the errors to get the desired result. Your code
HAS to produce a single output: 24.
Write a for loop that prints the odd numbers from 1 to 101, with one value on each line.
Compute and output
Evaluate and output
for
One of the easiest methods for numerically integrating functions is the trapezoidal method. To compute the integral, we evaluate the function at regular intervals, \(\Delta x\), starting from the lower bound and concluding with the upper bound and combining the result as
When computing integrals like this, it is best to determine
\(\Delta x\) from the number of evaluation point rather than
the other way around. I.e., if you wish to evaluate the integral from a
to b
using N=10
, then
N = 10
dx = (ba)/(N1)
This prevents problems with roundoff error when your interval is not precisely divisible by your \(\Delta x\).
Use the trapezoidal method to evaluate the integral
with N=11
and print the result.
In the code below, two \(3\times3\) matrices, mat1
and
mat2
, have been defined for you. Using a loops, multiply these together without
changing either one and assign the result to a variable called
mat3
.
Hint: you will need three nested loops.
Hint: it is easier if you create mat3
first as a \(3\times3\) of all zeros.
The below lists contain information on groceries: the SKU code, product category, brand, product name, price, and unit size.
Your program should allow the select a report to display. The reports should display:
The number of products from each brand.
A listing of all products with their SKU code, product name, price, unit size, and perunit cost.
The average perunit cost for each product category.
You can add additional features to the program if you like.
You may use features and modules not covered in class. If your code requires modules not supported by Runestone to run, please indicate that in a comment and list where you have successfully run your program.