Python Forum

Full Version: nested for loop dilemma 2
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Sep-12-2019, 12:21 PM)perfringo Wrote: [ -> ]I am afraid that you are very far from determining primes.

Maybe something along those lines:

def prime_checker(integer): 
    if integer == 2:                        # eliminate 2
        return True 
    if integer == 1 or integer % 2 == 0:    # eliminate 1 and all even integers
        return False 
    for i in range(3, integer, 2):          # divide only with odd numbers as integer can't be even
        if integer % i == 0: 
            return False 
    else: 
        return True

Very elaborate and sensible, I see now my very last attempts using if + and + or is rather flawed and the first if + for loop was closer to the truth however inefficient?

To tell the truth I haven't ever dug this deep in primes and my math skills I cannot brag about.
I just have so many tasks to get done so I am trying to rush through it without giving some of them the required thought like with this one.

I appreciate your input perfringo thanks for all the help.
(Sep-12-2019, 12:41 PM)YoungGrassHopper Wrote: [ -> ]I just have so many tasks to get done so I am trying to rush through it without giving some of them the required thought like with this one.

There are always couple of minutes to spare before you start coding. Think in simple terms - what you have, what you want etc. Otherwise - mess in your head will transform into mess in your code. For starters it's good to spell out plan or solution in spoken language and then translate it into Python. If you don't formulate what you want to accomplish you are not able to write code which delivers expected results.
(Sep-12-2019, 01:04 PM)perfringo Wrote: [ -> ]
(Sep-12-2019, 12:41 PM)YoungGrassHopper Wrote: [ -> ]I just have so many tasks to get done so I am trying to rush through it without giving some of them the required thought like with this one.

There are always couple of minutes to spare before you start coding. Think in simple terms - what you have, what you want etc. Otherwise - mess in your head will transform into mess in your code. For starters it's good to spell out plan or solution in spoken language and then translate it into Python. If you don't formulate what you want to accomplish you are not able to write code which delivers expected results.

Thanks for the golden advice perfringo it makes sense. And I believe its imperative that I start doing it now in the very early stages whilst laying the foundation in order to cultivate a sound coding mindset.

I apologise for all the cringing dumb mistakes I make, I hope that with practice and experience it will improve, until then please bare with my noobness haha and thanks for all the support.
Pages: 1 2