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))
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...
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F32a44685-1766-485d-931b-9939bfd22a88%2F3dda002a-d045-4d0a-844b-94a71712a6ae%2Fr3pszl_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
Step 1
CODE(SCREENSHOT) :
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.
Step by step
Solved in 2 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Recommended textbooks for you
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
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](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY