0

What is the error in it

In this question i need to generate the prime numbers between the given range?? Here's my try: class Prime: r = 0 def __init__(self,s,e): self.s=s self.e=e def func(self): re = [] for j in range(self.s,self.e): for k in range(1,j): if j%k==0: Prime.r = Prime.r+1 re.append(j) if Prime.r==1: return re a = Prime(int(input("Enter the start:")),int(input("Enter the end:"))) print(a.func())

2nd Jan 2022, 5:43 PM
Ravi King
1 Odpowiedź
+ 1
I rewrote it a little: class Prime: r = 0 def __init__(self,s,e): self.s=s self.e=e def func(self): re = [] for j in range(self.s,self.e): for k in range(1,j): if j%k==0: Prime.r = Prime.r+1 if Prime.r==1: re.append(j) Prime.r = 0 return re a = Prime(int(input("Enter the start:")),int(input("Enter the end:"))) print(a.func()) Most important, you forgot to reset Prime.r to 0 at the end of the second for loop.
2nd Jan 2022, 6:50 PM
Paul
Paul - avatar