Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested for loop dilemma 2
#11
(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.
Reply
#12
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#13
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Big O runtime nested for loop and append yarinsh 4 1,331 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  How do I add another loop to my nested loop greenpine 11 4,443 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,177 Nov-25-2020, 10:04 AM
Last Post: palladium
  dilemma with list comprehension spalisetty06 1 1,896 Aug-14-2020, 09:18 AM
Last Post: buran
  Nested loop indexing Morte 4 3,808 Aug-04-2020, 07:24 AM
Last Post: Morte
  Nested for loop not looping puttingwordstogether 0 1,672 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  Help: for loop with dictionary and nested lists mart79 1 1,834 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Nested Loop for user input Ads 2 3,524 Dec-30-2019, 11:44 AM
Last Post: Ads
  openpyxl nested for loop help rmrten 3 5,610 Oct-16-2019, 03:11 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020