+ 2

Why there is no output in my code ?

I wrote an code in python for checking prime number. But when I give a number as input into it, there is no output. I can not understand where is the problem. Can any one please explain it to me? (Thank you for your solution) https://code.sololearn.com/cTQwQaXzqMfU/?ref=app

29th Apr 2022, 11:46 AM
riasat arafin
riasat arafin - avatar
6 Answers
+ 2
I understand the way you tested if a number is divisible by another. I just want you to think in more efficient and robust ways. Hint: read about other arithmetic operators. If the user inputs an odd number, d is never incremented, so it doesn't reach 3. It's indeed good to have the code shorter, but the wise limit is readability. For a very simple one, like this, that's OK. But descriptive names help quite a lot in more complex code.
29th Apr 2022, 2:09 PM
Emerson Prado
Emerson Prado - avatar
+ 3
If the user inputs an odd number, none of the if conditions is ever met, so the code enters an infinite loop and the interpret aborts. If the user inputs an even number, the first if condition is met and the code prints "this is not a prime number". But then it increments the divider, and the if conditions are never met again. So, again, an infinite loop and execution aborted, so you don't see the output. Some brain food for you to find a solution: 1. Which should be the loop exit condition? If 'f' never changes, what's the point of 'f > 2'? 2. Inside the loop, when should the code increment the divider? 3. Why two consecutive 'ifs' with identical conditions? 4. If 'f/d - int(f/d) == 0', when will the code reach the 3rd 'if'? 5. How do you test if a number is divisible by another one? 6. What about more descriptive names for your variables?
29th Apr 2022, 12:19 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado thank you so much for dedicating some time to explain me. We know, a prime number is a number which is only divisible by 1 and the number itself. So, if I see that this number is divisible by any another number, it is not prime. So, I was checking if the number is fully divisible by any another number. that was like : (50/2= 25.0 - int(50/2)= 25 ) == 0; then 50 is fully divisible by 2. So, it is not prime. and: (25/10 = 2.5 - int(25/10) = 2) == not equal to 0. So, it may be prime and the code divides 25 by 10+1 == 11.
29th Apr 2022, 1:20 PM
riasat arafin
riasat arafin - avatar
0
yes, there was big fault as you told in 4 no point and I corrected it. But if user inputs an odd number like 15, when the d variable reaches 5, the first condition if fulfilled, so shouldn't it output: this is not a prime number ?
29th Apr 2022, 1:26 PM
riasat arafin
riasat arafin - avatar
0
I wrote variables here shorter as my wish to short the code.
29th Apr 2022, 1:27 PM
riasat arafin
riasat arafin - avatar
0
yes you are right. thank you so much. I can understand now
29th Apr 2022, 2:44 PM
riasat arafin
riasat arafin - avatar