\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Oct 24, 2024 | 376 words | 4 min read

7.2.2. Task 2#

Learning Objectives#

Read a flowchart that contains loop structures; Design a flowchart for a program with loop structures; Understand the execution sequence for a program with loop structures.

Introduction#

When writing programs, there will be times when you want to perform an operation multiple times, or you want to iterate over a set of data. One of the ways to accomplish this is by using a loop. Loops allow programmers to repeat lines of code multiple times in one run of a program.

Part A: Tracking Loops through Code#

The program in Listing 7.1 contains a nested loop (a loop inside another loop). Determine what will be displayed in the console when the program is run. When you are finished, write your answer in py3_team_teamnumber.pdf and save the file. Check this answer with a member of the instructional team. After this answer is right move onto the next problem.

Listing 7.1 Nested Loop Code#
x = 3
y = 6
i = 1
while i < 10:
    print(f"Starting while loop iteration: i = {i}, x = {x}, y = {y}")
    for z in [x, y]:
        i += z
        print(f"  Added {z} to i. New i = {i}")
    if x < 3:
        x += 1
        print(f"  Incremented x; Current x = {x}")
print(f"Final value of i: {i}")

Part B: Creating Programs with Loops#

  1. A common mathematical series is a factorial, which is often used in figuring out combinations of events. The definition of the factorial of a positive integer, \(N\), is defined as:

    (7.1)#\[N! = \prod_{n=1}^N n = 1 * 2 * 3 * ... * N\]
  2. Create a flowchart with a function that will calculate and return the factorial of a number passed as an argument. The function should return an error flag value (-999) if the number passed is negative, since the factorial function can only be calculated for non-negative integers. Save the flow chart in py3_team_teamnumber.pdf.

  3. Using the algorithm developed in your flowchart, write a Python program using the ENGR133_Python_Template.py that defines a function named my_factorial to compute the factorial of a number. In the main function of your program, prompt the user to enter a number and then call the my_factorial function to calculate the factorial of the number entered. Display the results based on the value returned from your my_factorial function. Save the program as py3_team_2_b_teamnumber.py.

Sample Output#

Use the values in Table 7.5 below to test your program.

Table 7.5 Test Cases#

Case

entry

1

5

2

8

3

0

4

-5

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 py3_team_2_b_teamnumber.py Enter a number: 5 The Factorial of 5 is 120.

Case 2 Sample Output

$ python3 py3_team_2_b_teamnumber.py Enter a number: 8 The Factorial of 8 is 40320.

Case 3 Sample Output

$ python3 py3_team_2_b_teamnumber.py Enter a number: 0 The Factorial of 0 is 1.

Case 4 Sample Output

$ python3 py3_team_2_b_teamnumber.py Enter a number: -5 Error -999 [Negative input].