Could you tell me the error in the code below?


#1


#2

if x < 2 is never reached, given you placed it inside the loop, which won’t run when x is less then 2.

which is why you get an error for x=0


#3

and what’s wrong in this piece of code?


#4

I suggest running the code in your head with x=3 which the error message says is the input, and see where you end up. You can also use print statements to print out what’s happening.


#5

got that! but here’s another error I’m getting now.


#6

why would you do n += 1? What is the value? the loop will handle the increase of n.

return None is in the error message, what does this mean?


#7

I don’t know what none error means. and when i remove n+=1 and ran the code . it still shows some error


#8

That’s still the same error message. Think for a while what your program should do when the input is 3, and then follow what your code does, find where it starts doing something differently from what you meant and then you’ll know what needs to be different

3 is prime, so your function should reach a statement that returns True, if you’re returning None, then you aren’t returning True, so you didn’t reach a statement returning True

Run the code in your head. Use prints to verify that you’re running it correctly if you’re not sure.


#9

By default, functions return None, thus, when we get this error message, it means that the return keywords you added, are not reached, what does this say about our program and its design?


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.