10. Exercises¶
Write a function
print_triangular_numbers(n)
that prints out the firstn
triangular numbers. A call toprint_triangular_numbers(5)
would produce the following output:1 3 6 10 15
(Hint: use a web search to find out what a triangular number is.)
Modify the walking turtle program so that rather than a 90 degree left or right turn the angle of the turn is determined randomly at each step.
Modify the turtle walk program so that you have two turtles each with a random starting location. Keep the turtles moving until one of them leaves the screen.
Modify the previous turtle walk program so that the turtle turns around when it hits the wall or when one turtle collides with another turtle.
Here’s the start of a program for a weight training app that coaches users on how much weight they should lift for each of these three lifts: squat, bench, and deadlift. The program begins by having the user lift only 10 pounds for each lift. Each time they complete a set for a particular lift and say they are ready for the next set, add 10 pounds to the weight of their previous set and print a message that this is the new weight they should lift. The sets are all done for one lift at a time. So, for example, a user might squat 10 pounds, then 20 pounds, then 30 pounds and then say they don’t want to keep doing that lift. In this case, they’ll now get a printed message to bench 10 pounds, and so on and so forth.
Some of the code is already included below, but you will need to fill in the rest of the
main
function to produce the following functionality:For each lift, beginning with the squat, the function
workout_coach
should be called with the name of the lift and the current weight. This function prints a message to the user like the following:Time to squat 10 pounds! You got this!
Keep calling
workout_coach
for as long as the user answers “yes” to the following question: “Keep doing the squat? Enter yes for the next set.” (Note that you will need to fill in the name of the lift depending on which lift in the iteration they are on.) You can do something like the following to combine strings and a variable to create the prompt string:
input_prompt = "Keep doing the " + lift + "? Enter yes for the next set."
- If the user answers with anything besides “yes” to the above question, then stop calling
workout_coach
for that particular lift and move on to repeat the above process for the next lift (unless it is the deadlift, which is the last lift and thus once the user decides to stop at this point the program quits). - There is one special case where you should stop calling
workout_coach
— no matter what the user responds — and that is when the current weight is greater than 200 pounds for the bench. You have not yet talked with a lawyer about your app and you don’t want to get sued if anyone has a mishap, so you’re not going to encourage them to lift more than that amount of weight on the bench press (which is the exercise that, done improperly and without a spotter, causes most gym accidents). It is okay to keep encouraging users to lift more than 200 pounds for the squat and the deadlift, though, so you don’t need to set an upper limit for those lifts.
Here is some example output from a program run:
Time to squat 10 pounds! You got this! Time to squat 20 pounds! You got this! Time to bench 10 pounds! You got this! Time to bench 20 pounds! You got this! Time to bench 30 pounds! You got this! Time to deadlift 10 pounds! You got this! Time to deadlift 20 pounds! You got this! Time to deadlift 30 pounds! You got this! Time to deadlift 40 pounds! You got this!
Write a program to remove all the red from an image.
For this and the following exercises, use the luther.jpg photo.
Write a function to convert the image to grayscale.
Write a function to convert an image to black and white.
Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows:
new_r = (R × 0.393 + G × 0.769 + B × 0.189) new_g = (R × 0.349 + G × 0.686 + B × 0.168) new_b = (R × 0.272 + G × 0.534 + B × 0.131)
Write a function to convert an image to sepia tone. Hint: Remember that RGB values must be integers between 0 and 255.
Write a function to uniformly enlarge an image by a factor of 2 (in other words, make the image twice as wide and twice as tall).
After you have scaled an image too much it looks blocky. One way of reducing the blockiness of the image is to replace each pixel with the average values of the pixels around it. This has the effect of smoothing out the changes in color. Write a function that takes an image as a parameter and smooths the image. Your function should return a new image that is the same as the old one but smoothed.
When you scan in images using a scanner they may have lots of noise due to dust particles on the image itself or the scanner itself, or the images themselves may be damaged. One way of eliminating this noise is to replace each pixel by the median value of the pixels surrounding it. Write a program to do this.
Graded Lesson Assignment¶
Write a course_grader
function that takes a list of test scores as its parameter. It will add up these test scores and calculate an average score. It should then return a message of "pass"
or "fail"
depending on these two conditions:
- If the average score is greater than or equal to 70 and no single test score is below 50, then return a message of
"pass"
. - If the average score is lower than 70 or at least one test score is below 50, then return a message of
"fail"
.
Some sample function calls with comments on what should be printed out are included in main
for testing purposes.
Ready to submit your code? Time to fork, and submit a Pull Request to: this Github Repository.