Before broadband internet and pocket-sized television studios, Jim Carey's The Truman Show popularized the notion of one's life exposed to the world through real-time broadcast.


Start Here: Jump to a section

wb_incandescentIn-Class looping essentials
color_lensLooping art
shuffle_onMini Project: Critical value guessing

In-Class looping principles

Let's compose a short program that simulates what Truman saw in his fabricated world. Create a looping program that generates this output.

sample loop program

Modify this program to do the following. These task are not really cumulative, so if you get stumped on one, move on to the next

  1. Create a "release valve" that will exit the loop BEFORE the 101st iteration if the user does, in fact, ask for more than 100 loops.
  2. Only display the "Man with flowers" on EVEN numbered loops. Hint: use the modulus (i.e. remainder) operator % to check for even or odd numbers. Even number will have a mod of zero when the mod of 2 is taken.
  3. Modify the loop's body to require the user press enter after each iteration before proceeding to the next one. If the user doesn't just press enter but rather presses the x key then enter, terminate the loop entirely and display the program exit marker.

4. Inner looping

Inside each loop, adjust your program to count from 1 up to the current iteration's count. You may want to use the print without a newline at the end print(number_variable, end="") to display characters on the same line with multiple calls to print. This screen cap shows the 8th and 9th iteration only.

loop output

arrow_upward back up to contents

Looping art

Hard-coded pyramid

Creating character-based figures with looping will hone your looping skills. Write a series of short programs that can produce the following types of figures. Then wire up user input to allow for customized figures.

Loop art piece 1

TIP 0: Modify how print ends its output. By default, a call to print() will add a new line each time it prints. In meeting this challenge, your loops may want to print a single character at a time and not jump to the next line. print("No return", end="") will display the characters No return and the imaginary cursor will stay on the same line to the right of the n until a regular (i.e. single string argument) call to print(). You can also use \n which is the special sequence for a new line (operating system specific) such as when you hit enter in a text editor. Try print("Three New\n\n\nLines") and study the output.

TIP 1: One tiny step at a time. Break this problem down into its smallest possible components. When the first simple component works as expected, then expand on that component.

TIP 2: Two loops, one nested in the other. Imagine building this pattern one character at a time, as you would type this out directly on a keyboard. Your cursor starts on the left of a row, and prints one character at time. After printing a single character, the cursor moves one position to its right. An enter character key press will result in a new line

A common solution to this design challenge is based on moving this cursor in two dimensions, horizontally and vertically each with its own while loop. One while loop responds to a horizontal counter and the other a vertical one.

sample program output pyramid

User controlled pyramid creation

Amend your original art program from above to allow the user to specify these parameters for your looping:

  1. The pyramid size which will control the total number of rows and also the total number of columns
  2. The character to print during even-numbered counter cycles
  3. The character to print during odd-numbered counter cycles
  4. The number of spaces to insert in between each printed character. Hint: you'll need to build a string typed variable that contains the user's specified number of space characters. You can do this with a little loop as well. You can add string characters just like you can add numbers, so I can add a space to a string variable called s by writing s = s + ' '. If you did this process in a loop, you'll make a custom-length string variable.
Loop art piece 2
sample program output pyramid

Extension exercises on looping

arrow_upward back up to contents

Mini-project: Critical Parameter Guessing

This mini project uses looping to create a program that can monitor its convergence on a specified target. The program will hard code or the user will enter the value of an interesting parameter, such as the speed at which an aircraft can lift off and fly (such as 57 knots per hour for a Piper Archer). Your program will then use random guessing to try to match the chosen value. We'll use looping to track how many tries the computer required to get within a given distance of our chosen value.

Project Specifications

Sample output

The following screen caps show a sample program run. MILES PER HOUR of a land vehicle is hereby TAKEN as a topic for your chosen variable. Perhaps: the frequency of your favorite note or key signature namesake note, the length of a pop song, the distance on a plane to your favorite destination.

program output

Meet random

This program will try to guess your chosen value. Its guesses will come from a tools somebody else wrote and shared with python programmers everywhere--the standard library.

random module

Python's standard library provides a module carrying all sorts of nifty tools for generating random floats and integers and even drawing from a known distribution curve

Check out the sample uses all the way at the bottom of the official function specifications. Try typing and running the most relevant sample uses of random in a test/tinker file to get comfy with how these tools work.

loop output

arrow_upward back up to contents

(c)2023 | content can freely reproduced according to the site's content use agreement.