Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python for else loop

adenn

New Coder
Hello today i wanna share about for ...else loop:
example: finding prime numbers
ans = int(input("number:"))
if ans >1:
for i in range(2,ans):
if ans%i==0:
print(f'{ans} is not a prime number')
break
else:
print(f'{ans} is a prime number.')
else:

print(f"{ans} is not a prime number")

What is for else loop?
when for loop hasn't been broke, else will happen

In this case:
I am trying to find prime number, so if ans is larger than 1(to make sure it isn't negative),
i will arrange a for loop, which let the range of 2 to ans(not include ans) to be divided by ans itself
If remaining value can be divided, then it is not a prime number, and the for loop will be break
if the for loop has not been broken, which means nothing can divide ans and get 0 remaining value
then it's a prime number, else will occur and print it's prime number
 
Hello adenn!

So, if I understand right, your post is a presentation of the for-else loop in Python?

Thank you for sharing your knowledge here!
Just a little remark: Please insert your code inside code tags by clicking on the </> button (And mark it as Python code) and make sure to keep the indentation, especially for Python code 😉
 
[CODE lang="python" title="prime.py"]primechecker = int(input("Number please,"))
if primechecker >= 2 or 3 or 5 or 7 or 11 or 13 or 17 or 19 or 23 or 29 or 31 or 37 or 41 or 43 or 47 or 53 or 59 or 61 or 67 or 71 or 73 or 79 or 83 or 89 or 97
print("Your input is a prime number")
if primechecker >= 4 or 6 or 8 or 9 or 10 or 12 or 14 or 15 or 16 or 18 or 20 or 21 or 22 or 24 or 25 or 26 or 27 or 28 or 30 or 32 or 33 or 34 or 35 or 36 or 38 or 39 or 40 or 42 or 44 or 45 or 46 or 48 or 49 or 50 or 51 or 52 or 54 or 55 or 56 or 57 or 58 or 60 or 62 or 63 or 64 or 65 or 66 or 68 or 69 or 70 or 72 or 74 or 75 or 76 or 77 or 78 or 80 or 81 or 82 or 84 or 85 or 86 or 87 or 88 or 90 or 91 or 92 or 93 or 94 or 95 or 96 or 98 or 99 or 100
print("Your input is not a prime number")
print("Script Worked")[/CODE]

Maybe this will work?
@adenn
 
[CODE lang="python" title="prime.py"]primechecker = int(input("Number please,"))
if primechecker >= 2 or 3 or 5 or 7 or 11 or 13 or 17 or 19 or 23 or 29 or 31 or 37 or 41 or 43 or 47 or 53 or 59 or 61 or 67 or 71 or 73 or 79 or 83 or 89 or 97
print("Your input is a prime number")
if primechecker >= 4 or 6 or 8 or 9 or 10 or 12 or 14 or 15 or 16 or 18 or 20 or 21 or 22 or 24 or 25 or 26 or 27 or 28 or 30 or 32 or 33 or 34 or 35 or 36 or 38 or 39 or 40 or 42 or 44 or 45 or 46 or 48 or 49 or 50 or 51 or 52 or 54 or 55 or 56 or 57 or 58 or 60 or 62 or 63 or 64 or 65 or 66 or 68 or 69 or 70 or 72 or 74 or 75 or 76 or 77 or 78 or 80 or 81 or 82 or 84 or 85 or 86 or 87 or 88 or 90 or 91 or 92 or 93 or 94 or 95 or 96 or 98 or 99 or 100
print("Your input is not a prime number")
print("Script Worked")[/CODE]

Maybe this will work?
@adenn
The main problem with this code is that it can only see if a number is prime for the first 100. It's better to be able to check any number, and generate any range of prime numbers on the fly if needed. I strongly advise against using a static list of numbers like this....

Also, if the first statement is not true, then you don't need the second if there. Just use if - else
 
The main problem with this code is that it can only see if a number is prime for the first 100. It's better to be able to check any number, and generate any range of prime numbers on the fly if needed. I strongly advise against using a static list of numbers like this....

Also, if the first statement is not true, then you don't need the second if there. Just use if - else
I tried but still got errors.
 
Back
Top Bottom