25-SEP-2023: Created initial looping page
Start Here: Jump to a section
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.
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
- 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.
- 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.
- 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.
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.
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.
User controlled pyramid creation
Amend your original art program from above to allow the user to specify these parameters for your looping:
- The pyramid size which will control the total number of rows and also the total number of columns
- The character to print during even-numbered counter cycles
- The character to print during odd-numbered counter cycles
- 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.
Extension exercises on looping
- Extension A: Inverted pyramid: Adjust your looping program to print an inverted pyramid, meaning the long base row is printed first, then the next row is the base minus one character, etc.
- Extension B: Fill lines with -: Adjust your program to fill the remaining space on each line with - characters, so the entire figure is a perfect square.
- Extension C: Duplicates: Allow the user to enter a duplicate count which would print each character x number of times as specified by the user. So in the above example, if the use entered 3 for the duplicate count, the third row of the pyramid would be @@@ ))) @@@
- Extension D: Row overflow buffer: Determine how many rows your output can print before wrapping to a new line. Insert logic in your program to automatically adjust the user's requested pyramid size to max out at the line length limit. Display a warning that your program is doing so.
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.
- User info: 1) Program asks the user for a value of a chosen parameter, such as the height of a weather balloon or speed of a vehicle. 2) Program asks the user for a tolerance or a “fudge room” from the chosen value the computer must be.
- Computation of computer guess and distance from target. 1) Program generates a guessed value in the range of your parameter. 2) Program computes the absoulte value (i.e. number line distance) between the guess and target value.
- Guess Success logic: Program correctly decides when the computer’s current guess is close enough to the target to be classied as a successful or close enough guess.
- Summary output: Once the computer’s guess was close enough to the target, the program displays the total number of guesses required and how far the successful guess was from the target. Optionally, program allows the user to repeat the process and input a parameter value and tolerance or to exit.
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.
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.
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.
(c)2023 technologyrediscogvery.net | content can freely reproduced according to the site's content use agreement.