+ 2

What is wrong here ?

n=int(input()) c=0 for i in range(1,n+1): for j in range(1,i+1): if i%j==0: c+=1 if c==2: print(i)

28th Jun 2019, 4:16 PM
Sajan Kumar Rajbanshi
Sajan Kumar Rajbanshi - avatar
7 Respuestas
+ 14
Your code is functional, but it's difficult to see what your goal is. j being always <= i, i%j always =0 so if your input is 2 or more, you'll have: i=1 --> j iters 1 => c=1 i=2 --> j iters 1,2 => c=3 and then c will keep growing, c will never be = 2 and thus nothing will ever be printed
28th Jun 2019, 4:33 PM
Cépagrave
Cépagrave - avatar
+ 7
You probably want to indent the last two lines (and fix your indentation in general)
28th Jun 2019, 4:27 PM
Anna
Anna - avatar
+ 6
Diego It helped Thanks alot
28th Jun 2019, 4:35 PM
Sajan Kumar Rajbanshi
Sajan Kumar Rajbanshi - avatar
+ 4
Fix your indentation and move "c = 0" after the first "for" loop. n = int(input()) for i in range(1,n+1): c = 0 for j in range(1,i+1): ...
28th Jun 2019, 4:30 PM
Diego
Diego - avatar
+ 4
Okay wait I will try
28th Jun 2019, 4:31 PM
Sajan Kumar Rajbanshi
Sajan Kumar Rajbanshi - avatar
+ 3
I didn't see that it's value wasn't renewing
28th Jun 2019, 4:35 PM
Sajan Kumar Rajbanshi
Sajan Kumar Rajbanshi - avatar
+ 3
Thanks anyway
28th Jun 2019, 4:35 PM
Sajan Kumar Rajbanshi
Sajan Kumar Rajbanshi - avatar