Dec 03, 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.
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#
Note
This function will be reused in Team Task 3.
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\]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.
Using the algorithm developed in your flowchart, write a Python program using the
ENGR133_Python_Template.py
that defines a function namedmy_factorial
to compute the factorial of a number. In themain
function of your program, prompt the user to enter a number and then call themy_factorial
function to calculate the factorial of the number entered. Display the results based on the value returned from yourmy_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.
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].