2.7: Looping Structures – WHILE Loops
- Page ID
- 206265
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)hypothes.is tag: s20iostpy07ualr
Download Assignment S2020py07
Learning Objectives
Students will be able to:
Content:
- Explain the three parts of a loop
- Explain the syntax of a while loop
- Explain sentinel-controlled and counter controlled loops
- Explain short-cut operators
Process:
- Write code that includes sentinel-controlled and counter controlled loops
- Write code that uses short-cut operators
Prior Knowledge
- Python concepts from Activities 1-6
- Understanding of flowchart input symbols
Further Reading
Model 1: While loops
A looping structure allows a block of code to be repeated one or more times. A while loop is one of the two looping structures available in Python
Python Program 1 |
Output |
|
Outside of loop. I am in a while loop. I am in a while loop. I am in a while loop. I am in a while loop. I am in a while loop. I am in a while loop. I am in a while loop. I am in a while loop. I am in a while loop. I am in a while loop. Outside of loop. |
Critical Thinking Questions
1. Closely examine the flowchart and Python program in Model 1.
- In the Python code, circle all the code associated with the WHILE loop.
- Enter and test the code. What does the line of code: counter = counter +1 do?
- How does the Python interpreter know what lines of code belong to the loop body?
- Every loop structure requires three actions. In the space below, write the line of code in the Python program that corresponds to each of the three actions.
- Initialize a variable used in the test condition:
- Include a test condition that causes the loop to end when the condition is false:
- Within the loop body, update the variable used in the test condition:
2. Enter and execute the following code using input values where number
- number <10
- 10< number < 20
- number=20
- 20< number < 30
- Number > 3
Python Program 2 |
---|
|
a. Beside each line of code above explain what the code does.
b. How do you change a print statement so that it does not signal the computer to write on a new line?
3. The following code should print the numbers from 1 to 10, but it does not print anything. Correct the problem
Python Program 3 |
---|
|
4. Enter and execute the following code:
Python Program 4 |
---|
|
NOTE: <ctrl + c> will cause a "Keyboard Interrupt" and stop a runaway program, as will the stop sign on the Thonny Interface |
5. Enter and execute the following code:
Python Program 5 |
---|
|
- State the output.
- What caused the output to display on one line?
- What two control structures are used in this code?
6. The following directions will create a program that prompts the user to enter a number between 1 and 10. As long as the number is out of range the program re-prompts the user for a valid number. Complete the following steps to write this code.
- Write a line of code the prompts the user for number between 1 and 10.
- Write a Boolean expression that tests the number the user entered by the code in step “a.” to determine if it is not in range.
- Use the Boolean expression created in step “b.” to write a while loop that executes when the user input is out of range. The body of the loop should tell the user that they entered an invalid number and prompt them for a valid number again.
- Write a line of code that prints a message telling the user that they entered a valid number.
- Put the segments of code from steps “a-d” together. Enter and execute the code. Does it work properly? If not, correct it and test it again.
- How many times does the loop execute?
Note
A looping structure for which you know the number of times it will execute is known as a count-controlled loop. In a sentinel-controlled loop there is a "signal" or "flag" value that tells the loop to end, and so you do not know how many times it will loop.
7. Sometimes a programmer does not know how many times data is to be entered. For example, suppose you want to create a program that adds an unknown amount of positive numbers that will be entered by the user. The program stops adding numbers when the user enters a zero or a negative number. Then the program prints the total. Before creating this program, review the three actions required for all loops:
- Initialize a variable that will be used in the test condition:
What will be tested to determine if the loop is executed or not? Write a line of code that initializes a variable to be used in the test condition of the loop for this program. The variable should contain a value entered by the user. - Include a test condition that causes the loop to end when the condition is false:
What is the test condition for the while loop used in this program? - Within the loop body, update the variable used in the test condition:
Write the code for the loop body. Include the code to update the variable in the test condition. - Is this a count-controlled loop?
Why or why not? - Complete the program.
Enter and execute the code.
NOTE: Short-cut operators provide a concise way of creating assignment statements when the variable on the left-hand side of the assignment statement is also on the right-hand side. The addition short-cut operator i(+=) is usually used for incrementing a variable. |
8. Enter and execute the following code:
Python Program 6 |
---|
|
- What does the “+=” shortcut operator do?
- The code: x += 5 is equivalent to which of the following lines of code?
- x = 5
- x = y + 5
- x = x + 5
- y = x + 5
c. Replace the operator ‘+=’ with the following shortcut operators and execute the code. Explain what each operator does.
- -=
- *=
9. Enter and execute the following code:
Python Program 7 |
---|
|
a. What is the output of the preceding code? Is it what you expected?
b. Rewrite the code so that it produces valid output.
c. Is the following line of code valid: 23 += total? Why or why not?
10. The following code should print the numbers beginning with 100 and ending with 0. However it is missing a line of code. Write the line of missing code using the shortcut operator so the program works.
Python Program 8 |
---|
|
11. Enter and execute the following code:
Python Program 9 |
---|
|
a. What does the program do?
b. What is the variable used to store the user’s input?
c. In the print statement, what does word[0]represent?
d. If you changed 0 to 1 in word[0]in the print statement above, what do you think will be printed?
e. When does the program end?
g. Why do you think this the loop in this program is an example of a sentinel control loop?
h. Examine the print statement in this program:
print("First letter of " + word + " is " + word[0])
What is the purpose of the “+” as part of the argument in the print statement? How would the output change if you replace each “+” with a “,”
Information: A variable that can store only the values True and False is called a Boolean variable. The type() function can be used to tell you what class (Boolean, integer, floating point, or string) of data is stored in a variable |
12. Examine the code below.
name = "Simone"
cost = 3.56
numApples = 89
foundCost = False
What class of data is stored in each variable: (Boolean, integer, floating point, or string)
-
- name –
- cost –
- numApples –
- foundCost –
NOTE: To check your answers above, you can type each of the variables into your python shell, and then use the type() function to identify the class of data. |
13. Interpret the following code:
a. What does the program do?
b. What type of variable is ‘doAgain’?
c. What does the following code in the program do?
if another != 'y':
doAgain = False
CopyRight Statement
This work is licensed under a Creative Commons Attribution-NonCommercial-Shar
Application Questions: Use the Python Interpreter to check your work
1. Write a code segment that prompts the user for an even number. As long as the number is not even, the user should be given a message and prompted again for an even number.
2. Write code segment that prompts the user for a letter from ‘a-z’. As long as the character is not between ‘a-z’, the user should be given a message and prompted again for a letter between ‘a-z’.