98-381 Exam QuestionsBrowse all questions from this exam

98-381 Exam - Question 6


DRAG DROP -

You are building a Python program that displays all of the prime numbers from 2 to 100.

How should you complete the code? To answer, drag the appropriate code segments to the correct location. Each code segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Exam 98-381 Question 6
Show Answer
Correct Answer:
Exam 98-381 Question 6

References:

https://docs.python.org/3.1/tutorial/inputoutput.html

https://stackoverflow.com/questions/11619942/print-series-of-prime-numbers-in-python https://www.programiz.com/python-programming/examples/prime-number-intervals

Discussion

6 comments
Sign in to comment
devpool
Apr 20, 2021

p=2 while p<=100: is_prime=True for i in range(2, p): if p % i == 0: is_prime=False break if is_prime == True: print(p) p=p+1

Dkadet159958
Nov 30, 2020

why is_prime = True it needs to be inside while loop, can someone explain please.

sadako11
Dec 7, 2020

is_prime is what is called a flag. In our program its main purpose is to control which number to print. In our case only prime numbers when is_prime = True. if is_prime==True: print(p) When is_prime is set to False (when a non prime number is found) then the statement if is_prime==True: print(p) is not executed. is_prime must be located inside the while loop to reset its value for each number. If it is located outside the while loop it will not reset and will get stuck with the same value throughout the complete while loop. When the loop finds a non primal number (is_prime==False) is_prime will be stuck with False for the whole while loop and the code if is_prime==True: print(p) will never be executed.

PavanKumpatlaAccountTwo
Jan 1, 2022

The code asks for prime numbers between 2-100. So we use the while loop with "<=" so that the number is less than or equal to 100. After all, we don't want the number to be over 100 do we? And we use the while loop because it will repeatedly check until the condition is true.

yasminek
Feb 21, 2021

continue

cono
Nov 6, 2020

Would you not use 'continue' rather than 'break' so loop can be repeated?

Vaibhavdhingra24
Jun 24, 2020

Can someone explain the indentation??

FuzzyDunlop
Jul 7, 2020

The indentation represents the literal indentation you would use when writing the python code. The for loop is within the while loop and the break statement is within the if statement.

Shanmahi
May 30, 2021

p = 2 while p <= 100: is_prime = True for i in range(2, p): if p % i == 0: is_prime = False break if is_prime: print(p) p = p+1

gargaditya
Oct 11, 2020

Perfect answer,be careful with the 'break' statement which breaks the inner for loop the moment a non prime number is detected(after setting is_prime to false) Also note difference between division(/) and remainder after division(%) logic: eg a number like 6 start with 2,test divisibility by 2, 3,4,5 number like 10 start with 2,test divisibility by 2,3,4....9 If anywhere remainder(ie modulo operation->%) is zero ie fully divisble,then mark non prime and break further testing