What change should be made to fix the code: def primes(up_to): result = [] for n in range(2, up_to): for x in range(2, n): # (A) # (B) # (C) if n % x == 0: break # (D) result.append(n) else: return result print(primes(10))

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Please see attachment

Delete the break at line
The following code has an error. It is currently
printing:
[3, 5, 5, 5, 7, 7, 7, 7, 7, 9]
Align the indent of the else block at line D with the
for at
And it should be printing:
line A
[2, 3, 5, 7]
What change should be made to fix the code:
Change the if condition at line
to:
def primes (up_to):
result = []
for n in range(2, up_to):
for x in range(2, n):
if n % x !- 0
# (A)
# (B)
if n % x == 0:
break
# (C)
else:
# (D)
result.append (n)
return result
Change the if condition at line
to:
print(primes(10))
if x % n == 0
Transcribed Image Text:Delete the break at line The following code has an error. It is currently printing: [3, 5, 5, 5, 7, 7, 7, 7, 7, 9] Align the indent of the else block at line D with the for at And it should be printing: line A [2, 3, 5, 7] What change should be made to fix the code: Change the if condition at line to: def primes (up_to): result = [] for n in range(2, up_to): for x in range(2, n): if n % x !- 0 # (A) # (B) if n % x == 0: break # (C) else: # (D) result.append (n) return result Change the if condition at line to: print(primes(10)) if x % n == 0
Expert Solution
Step 1

CODE(SCREENSHOT) :

 Computer Engineering homework question answer, step 1, image 1

Explanation : 

in line 1 , function is defined.

in line 2 , an empty list is declared.

in line 3 , a for loop is used which goes from 2 to the number.

in line 4 , another for loop is used which goes from 2 to the previous loop variable 'n'.

in line 5 , check if n is divisible by any number from 2 to that number - 1 than break out of loop.

in line 6 , an else statement is used with the second for loop to append the prime number to list.

in line 10 , the list is returned.

in line 12 , the function is called and the list is printed. 

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY