python sum if prime [closed]
up vote
-6
down vote
favorite
I need to write a program that the user input numbers and stops when the user enter 0, them it should see which number is a prime number and sum only the prime numbers.
I wrote it but it counts even if the number isn't a prime.
please advise
python sum
closed as off-topic by gnat, Cindy Meister, mpromonet, Unheilig, Graham Nov 12 at 0:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Cindy Meister, mpromonet, Graham
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-6
down vote
favorite
I need to write a program that the user input numbers and stops when the user enter 0, them it should see which number is a prime number and sum only the prime numbers.
I wrote it but it counts even if the number isn't a prime.
please advise
python sum
closed as off-topic by gnat, Cindy Meister, mpromonet, Unheilig, Graham Nov 12 at 0:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Cindy Meister, mpromonet, Graham
If this question can be reworded to fit the rules in the help center, please edit the question.
3
We cannot read the tiny image and will not follow random external links. Please insert the actual code of your program in the post.
– DYZ
Nov 11 at 6:52
2
Can you please post the code as is here? Helps copying and testing. Thanks.
– Sid
Nov 11 at 6:53
2
Please post your code, not the image.
– Sanchit Kumar
Nov 11 at 6:53
1
What do you thinkif False:
is going to do, ever? Or whatif True:
is going to do, never? :^)
– Mark Tolonen
Nov 11 at 6:54
1
Why not upload images of code on SO when asking a question?
– Ayxan
Nov 11 at 7:19
add a comment |
up vote
-6
down vote
favorite
up vote
-6
down vote
favorite
I need to write a program that the user input numbers and stops when the user enter 0, them it should see which number is a prime number and sum only the prime numbers.
I wrote it but it counts even if the number isn't a prime.
please advise
python sum
I need to write a program that the user input numbers and stops when the user enter 0, them it should see which number is a prime number and sum only the prime numbers.
I wrote it but it counts even if the number isn't a prime.
please advise
python sum
python sum
edited Nov 11 at 6:52
DYZ
24.3k61948
24.3k61948
asked Nov 11 at 6:51
Lior Boostany
1
1
closed as off-topic by gnat, Cindy Meister, mpromonet, Unheilig, Graham Nov 12 at 0:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Cindy Meister, mpromonet, Graham
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by gnat, Cindy Meister, mpromonet, Unheilig, Graham Nov 12 at 0:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Cindy Meister, mpromonet, Graham
If this question can be reworded to fit the rules in the help center, please edit the question.
3
We cannot read the tiny image and will not follow random external links. Please insert the actual code of your program in the post.
– DYZ
Nov 11 at 6:52
2
Can you please post the code as is here? Helps copying and testing. Thanks.
– Sid
Nov 11 at 6:53
2
Please post your code, not the image.
– Sanchit Kumar
Nov 11 at 6:53
1
What do you thinkif False:
is going to do, ever? Or whatif True:
is going to do, never? :^)
– Mark Tolonen
Nov 11 at 6:54
1
Why not upload images of code on SO when asking a question?
– Ayxan
Nov 11 at 7:19
add a comment |
3
We cannot read the tiny image and will not follow random external links. Please insert the actual code of your program in the post.
– DYZ
Nov 11 at 6:52
2
Can you please post the code as is here? Helps copying and testing. Thanks.
– Sid
Nov 11 at 6:53
2
Please post your code, not the image.
– Sanchit Kumar
Nov 11 at 6:53
1
What do you thinkif False:
is going to do, ever? Or whatif True:
is going to do, never? :^)
– Mark Tolonen
Nov 11 at 6:54
1
Why not upload images of code on SO when asking a question?
– Ayxan
Nov 11 at 7:19
3
3
We cannot read the tiny image and will not follow random external links. Please insert the actual code of your program in the post.
– DYZ
Nov 11 at 6:52
We cannot read the tiny image and will not follow random external links. Please insert the actual code of your program in the post.
– DYZ
Nov 11 at 6:52
2
2
Can you please post the code as is here? Helps copying and testing. Thanks.
– Sid
Nov 11 at 6:53
Can you please post the code as is here? Helps copying and testing. Thanks.
– Sid
Nov 11 at 6:53
2
2
Please post your code, not the image.
– Sanchit Kumar
Nov 11 at 6:53
Please post your code, not the image.
– Sanchit Kumar
Nov 11 at 6:53
1
1
What do you think
if False:
is going to do, ever? Or what if True:
is going to do, never? :^)– Mark Tolonen
Nov 11 at 6:54
What do you think
if False:
is going to do, ever? Or what if True:
is going to do, never? :^)– Mark Tolonen
Nov 11 at 6:54
1
1
Why not upload images of code on SO when asking a question?
– Ayxan
Nov 11 at 7:19
Why not upload images of code on SO when asking a question?
– Ayxan
Nov 11 at 7:19
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
First of all, you are putting the definition inside the while loop, this will not disrupt your code, but make it significantly slow.
Second, you are using statements like if False
, that will ALWAYS return False
and the code wont run
So, instead of using if False
try if not prime(num)
, then put an else
statement for the other half. You don't even need the sum_of_primes = 0 +sum_of_primes
, you can just remove that so you can finally replace the if-else
with:
if prime(num):
sum_of_primes = sum_of_primes + num
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
First of all, you are putting the definition inside the while loop, this will not disrupt your code, but make it significantly slow.
Second, you are using statements like if False
, that will ALWAYS return False
and the code wont run
So, instead of using if False
try if not prime(num)
, then put an else
statement for the other half. You don't even need the sum_of_primes = 0 +sum_of_primes
, you can just remove that so you can finally replace the if-else
with:
if prime(num):
sum_of_primes = sum_of_primes + num
add a comment |
up vote
1
down vote
First of all, you are putting the definition inside the while loop, this will not disrupt your code, but make it significantly slow.
Second, you are using statements like if False
, that will ALWAYS return False
and the code wont run
So, instead of using if False
try if not prime(num)
, then put an else
statement for the other half. You don't even need the sum_of_primes = 0 +sum_of_primes
, you can just remove that so you can finally replace the if-else
with:
if prime(num):
sum_of_primes = sum_of_primes + num
add a comment |
up vote
1
down vote
up vote
1
down vote
First of all, you are putting the definition inside the while loop, this will not disrupt your code, but make it significantly slow.
Second, you are using statements like if False
, that will ALWAYS return False
and the code wont run
So, instead of using if False
try if not prime(num)
, then put an else
statement for the other half. You don't even need the sum_of_primes = 0 +sum_of_primes
, you can just remove that so you can finally replace the if-else
with:
if prime(num):
sum_of_primes = sum_of_primes + num
First of all, you are putting the definition inside the while loop, this will not disrupt your code, but make it significantly slow.
Second, you are using statements like if False
, that will ALWAYS return False
and the code wont run
So, instead of using if False
try if not prime(num)
, then put an else
statement for the other half. You don't even need the sum_of_primes = 0 +sum_of_primes
, you can just remove that so you can finally replace the if-else
with:
if prime(num):
sum_of_primes = sum_of_primes + num
edited Nov 11 at 7:02
answered Nov 11 at 6:56
ѕняєє ѕιиgнι
30014
30014
add a comment |
add a comment |
3
We cannot read the tiny image and will not follow random external links. Please insert the actual code of your program in the post.
– DYZ
Nov 11 at 6:52
2
Can you please post the code as is here? Helps copying and testing. Thanks.
– Sid
Nov 11 at 6:53
2
Please post your code, not the image.
– Sanchit Kumar
Nov 11 at 6:53
1
What do you think
if False:
is going to do, ever? Or whatif True:
is going to do, never? :^)– Mark Tolonen
Nov 11 at 6:54
1
Why not upload images of code on SO when asking a question?
– Ayxan
Nov 11 at 7:19