+ 1
What is the problem with this code
for n in range(2,10): for x in range(2,n): if n%x ==0: print(n,'equal to',x,'*',n//x) break else: print(n,'is a prime number') It print too many prime number. When i add break in the last line, the code execute in the first codition and also return 9 is a prime number , which isn't.
3 ответов
+ 3
Add a little extra debug:
# last line
print(n,'is a prime number ( x:',x,')')
3 is a prime number ( x: 2 )
4 equal to 2 * 2
5 is a prime number ( x: 2 )
5 is a prime number ( x: 3 )
5 is a prime number ( x: 4 )
6 equal to 2 * 3
7 is a prime number ( x: 2 )
7 is a prime number ( x: 3 )
7 is a prime number ( x: 4 )
7 is a prime number ( x: 5 )
7 is a prime number ( x: 6 )
8 equal to 2 * 4
9 is a prime number ( x: 2 ) # but...you haven't run all the tests yet
9 equal to 3 * 3
+ 1
add a check for each prim which would try to divide the number in a whole but since it's a prime number it can't so it would be put in a boolean witch would count it as true
0
Okay. But i still feel a little bit extra. How can i cut down these multiple line