What am I doing wrong…I dont want to look at the solution, I really want to solve this one, but I get an error.
This is my code:
def is_prime(x):
if x / 1 or x / x == 1:
return True
else:
return False
is_prime(5)
What am I doing wrong…I dont want to look at the solution, I really want to solve this one, but I get an error.
This is my code:
def is_prime(x):
if x / 1 or x / x == 1:
return True
else:
return False
is_prime(5)
It would be helpful if you also posted the error, rather than simply saying you’re getting one.
In the interim, I am going to assume that by “error” you mean you’re not passing the exercise. This is because of two reasons:
True
.The exercise asks you to define a function is_prime
which takes a single input, x
. You’re then asked to check whether x
is evenly divisible by another number, n
, for every value of n
between 2
and x - 1
. You’ve got the def
statement right, but the body of your function isn’t right.
Also, your if
statement is flawed. Let’s say we call your function as is_prime(6)
. 6
is not a prime number, because it is divisible by both 2
and 3
, so your function ought to return False
.
Your if
evaluates out, in this case, to 6 / 1 == 1
or 6 / 6 == 1
. Six divided by six is one, so the right-hand side of your or
statement is True
and your function returns True
. The problem is that x / x
will always equal 1
, so you’re always going to return True
for any value of x
regardless of whether it’s prime.
You need to work out the steps required to meet the requirements, and then convert those steps into code.
Im sorry here is the error:
Your function crashed because you were trying to divide or modulo (%) by zero.
Oh you are right, didn’t think abou that…
Not sure where that’s coming from… Nothing in the code you posted above will result in a divide by zero. (It runs fine on repl.it)
Still, your function doesn’t do what it’s supposed to in that it doesn’t verify if a number is prime.
Then I dont know, thats what it show me…but anyaway, I had to look at the solution, and not a chance that I would have guessed that thats the way you should write the code…so I’ll just use the codecademy solution, and I’ll use the next few hours to decode whats happening inside the codecademy code…
If the Codecademy solution makes as little sense to you as you’re suggesting, then I really do have to ask - do you at least understand the math that underpins this exercise?
Take a step back and work out the steps that you’d need to perform to check whether a given number is prime if you were doing the calculations by hand on a piece of paper.
If you can work out the logical steps to verify whether a number is prime by hand, you’ll understand the Codecademy solution but it might not make a great deal of sense if you don’t understand the math. Feel free to ask here if there’s anything in particular that stumps you.
The thing is the codecademy solutions make sense, I can see what happens in the code, but the problem is that I have problems with coming up with the code my self, and then I end up looking at the solution(which I usually understand), but that feels like cheating you know, and that bothers me…I want to be able to write the code my self…
Yes, the thing is I get what prime number is now, but reading the instructions and putting the code together is what gives me hard time…
Thank you, appreciate it!
Right now Im going through all of my notes so far in the Python course, just to get a little refresher of the different topics…hopefully that will help a little…
In very broad terms, all we’re doing when we write code is getting the computer to follow a logical process to its conclusion. We’re telling the computer what steps to take, what decisions to make, and how to proceed based on those decisions. If we can’t write out the steps we need to take in plain English (other languages are available) to achieve the desired outcome, we’re not going to be able to write code that does it either.
Let’s take the is_prime
function as an example here. You can click the header below if you want to see the instructions from the exercise environment.
We need to create a function that does what we’re asked - check whether a number, x
, is prime.
How would we do this, if we were sat at a desk armed with only a pen and paper?
We know from the definition of a prime number that x
is prime if it’s a positive integer greater than one, and which can only be evenly divided by 1 and itself. Using this, we know that any number less than 2 cannot be prime.
We can also deduce from this that 2 must be prime, since it’s the smallest positive integer that’s greater than one. It only has two positive factors, 1
and 2
, and so is prime. We don’t need to calculate anything here, we can just say “Yes, 2 is prime!”.
We can further deduce that any even number is not prime, because if it’s an even number it must be divisible by 2 and so by definition is not prime and we’ve just cut the amount of math we need to do in half.
So, to check any larger odd number, we need to do some math - but let’s not overdo it. We can optimise the math so we don’t do more than we need to.
For arguments sake, let’s say we’re checking whether x = 763
is a prime number.
Since the number is odd, we know immediately that it won’t divide by 2
without leaving a remainder so we don’t need to bother dividing by 2. We also don’t need to bother dividing by 4, or 6, or 8 and so on… because if it’ll divide evenly by a multiple of 2, it’ll also divide evenly by 2.
So, what we need to do is check whether 763
will divide evenly by any of the odd integer numbers larger than 1. Let’s do it.
763 / 3 = 254.333333
763 / 5 = 152.600000
763 / 7 = 109.000000 !!
As you can see, 763
doesn’t divide evenly by 3
or 5
but it does divide evenly by 7
so we know it’s not prime. We don’t need to carry on any further than this, as this is sufficient to demonstrate that 763
isn’t a member of the prime numbers club.
Condensing this down, we can see that the decisions we’ve taken are as follows:
x
less than or equal to 1? If yes, definitely not prime.x
equal to 2? If yes, definitely prime.x
an even number, that divides by 2
with no remainder? If yes, definitely not prime.x
divide evenly by any odd number, n
, that is between 3 and itself? (So n
can take any value n = 3, 5, 7, 9.... x
). If we find any value of n
where there’s no remainder, we know x
is not prime and we can stop calculating. If not, it’s prime!Do you see what I mean about writing out the steps to solve the problem, and can you take the steps I’ve given you and convert them into a Python function that meets the requirements for is_prime
? (Or, looking at it the other way, does this help you understand the solution that Codecademy gave for this exercise?)
If you’re super stuck, pay attention to the way I’ve written the steps. For example:
- Is the number
x
equal to 2?If
yes, definitely prime.
The questions you should be asking, and any repetitive or sequential actions you need to carry out, will more than likely correspond to a programming structure you’ve learned in the Codecademy course.
Thanks for the awesome explanation!
But sometimes is little things like
For each number n from 2 to x - 1
that confuse me in the Codecademy instructions…
For each number n, I can see that we need to use for loop
From 2 to x - 1, I can see that we need to use range()…but that is because I saw it in the solution, but I understand why they used it, so thats all good too…
BUT, x - 1…??? That confuses me a whole lot!!! Lets say my input was the number 5…so x = 5…
but that little x - 1 will turn my number to 4…?!?
Ok I thoght a little bit about that line that I just wrote…and the reason they do that is because they want us to check every number from 2 up to our number, but to not include our number…lol
The author meant figuratively, not literally. The top of the range will be x - 1 when,
range(2, x)
Only if n is greater than 2, hence the range written as,
range(2, x)
For the record, the current approach is a brute foce solution. It checks every number from 2 to x-1. We can see that there is a lot of checking that amounts to nothing. Anything more than x/2 is moot. In fact, anything over x ** 0.5 is moot. But this is not the place to talk about that. Solve this problem with the simplest possible approach using brute force. That’s the goal.
This is the Codecademy solution:
def is_prime(x):
if x < 2:
return False
else:
for n in range(2, x-1):
if x % n == 0:
return False
return True
Im not sure if Im getting this…so the x-1 in the solution is unnecessary…should just write x instead of x-1…?
It is completely valid to write it that way, so yes, for this brute force approach that would be the natural way.
Ok, but why do they choose to write x-1 then…why not just write x if it does the job…and what are they trying to accomplish with x-1?
It was an oversight. This little ditty has been plaguing learners for years. All for no reason at all. It just confuses them and derails the lesson.
Aaah man, they should fix it Jeeesus hehe…I’ll go and change it my notes too, thanks for notifying me
I agree that the phrasing of the range for n
is confusing, but keep in mind how the range()
function works.
If we do range(2,10)
for example, we get [2, 3, 4, 5, 6, 7, 8, 9]
. Using x
as the upper limit in range
ensures that we don’t accidentally divide our number by itself and wrongly rule it out as prime, but we are still checking every divisor in the range 2
to x - 1
and so that wording remains valid.
I suspect that the people who wrote the exercise made an assumption on a couple of things.
Glad to hear that you’ve figured out the exercise, though.
If they put it there to remind us that the number that is x will not be included when we use range() is fine with me, but they should rephrase the instruction and definitely not put it in the code like they did hehe
And I dont want to sound harsh towards Codecademy, I think they are great, but things like that might be off putting to people and make them switch to another service where they can learn to code the “right” way…
Yeah, I agree; though whilst using x - 1
will mean your range of values for n
doesn’t actually include the value of x-1, mathematically it won’t affect the outcome if the rest of the code is correct.
It won’t?
Let say my number is 10, and we use range (2, x), my range will be 2 to 10 = 2,3,4,5,6,7,8,9
But lets say my x is 10 again, but we use range(2,x-1), wouldn’t it make the range 2 to 9 then? = 2,3,4,5,6,7,8