Question

Using nested loops, write a function called primes(a,b) that takes in two positive integers a and...

Using nested loops, write a function called primes(a,b) that takes in two positive integers a and b (where a<b). Then simply RETURNS a string containing all the prime numbers between a and b (or if there are none, the string "No Primes"). You should check that a and b are valid inputs, that is, that a and b are integers such that a<b (otherwise, the function should print “No Primes”). Three sample calls of your function (in IDLE) should produce the following results (please be sure to format your output exactly as shown and please note that we are not printing them into terminal):

```>>> primes(12,72)
'Primes: 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71.'
>>> primes(3,10)
'Primes: 3, 5, 7.'
>>> primes(10,4)
'No Primes'
>>> primes(8,10)
'No Primes'```

Explanation:

Here is the function primes which takes two integers a and b

And returns the string of prime numbers between a and b

Code:

def primes(a, b):

res = []

if(a>=b):
return 'No Primes'

for i in range(a, b+1):

is_prime = 1

for j in range(2, i):
if(i%j==0):
is_prime=0
break

if(is_prime==1):
res.append(i)

if(len(res)==0):
return 'No Primes'
else:
s = ''

for i in range(len(res)):
s = s + str(res[i])
if(i!=len(res)-1):
s = s+ ', '
else:
s = s+'.'
return s

print(primes(12, 72))
print(primes(3, 10))
print(primes(10, 4))
print(primes(8, 10))

Output:

PLEASE COMMENT IF YOU NEED ANY HELP!

Earn Coins

Coins can be redeemed for fabulous gifts.