Jan-28-2022, 03:41 PM
Frankduc Wrote:Wonder what is the principle idea behind the scene to be force to create a new variable to escape the indent.Your design is wrong from the start because the role of function
is_prime_v1()
is not well defined. It tells whether n
is prime by returning a boolean, and at the same time, it appends items to the prime
list and returns the value of n
. These are too many tasks for a single function.def is_prime_v1(n): """Return a boolean indicating if integer n is prime""" if n == 1: return False for d in range(2, n): if n % d == 0: return False return True prime = [] for n in range(1,21): if is_prime_v1(n): prime.append(n) print(prime)
Output:[2, 3, 5, 7, 11, 13, 17, 19]