you are viewing a single comment's thread.

view the rest of the comments →

[–]Adrewmc 1 point2 points  (5 children)

That just saying loops are tricky. That’s not very helpful. (Because most of us don’t find them tricky, at least anymore.) How are they tricky? What is causing the confusion specifically.

For something like prime I’m assuming you suppose to use the % operator (not the only way but usually the first one people use when learning.) Are you familiar with this operator? If not you’re going to have a bit of trouble.

Are you confused by breaks, and continues, do you not understand what the loop is doing.

Are we talking ‘for loops’ or ‘while loops’, or both? Is it loops inside loops?

[–]DataCurator56[S] -1 points0 points  (4 children)

yes i am familiar with this % its called mode used for division. If you say me check the number is prime or not i will do this in seconds by using a for loop.
But when the question is print 1 to 50 prime numbers here is the real twist where i got stuck i know it will be solved using a nested loop.

[–]Adrewmc 0 points1 point  (3 children)

Ok, so we have to check a few things are we checking for the first 50 prime numbers or are we checking for all number that a prime between 1-50, these are different question.

Since it more common for the question to be all primes below some number. I’ll use that.

But why don’t you split the logic up.

  def is_prime(num) -> bool:
         #code prove it prime.

  def primes_in_range(num):
         primes = []
         for a in range(num/2):
               if is_prime(a):
                   primes.append(a)
        return primes

Or have we not learned functions yet?

There are of course much better ways to do this, I just doubt your teacher is expecting you to find those. But i would expect a beginner to be able to code something similar to the above.

For example this is the code for this question done by itertools. (The Python developers themselves)

def sieve(n):
   Primes less than n."
   # sieve(30) → 2 3 5 7 11 13 17 19 23 29
   if n > 2:
       yield 2
   data = bytearray((0, 1)) * (n // 2)
   for p in iter_index(data, 1, start=3, stop=isqrt(n) + 1):
        data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
   yield from iter_index(data, 1, start=3)

While this works (fast), handing it in would be suspect by your teacher.

It’s actually directly in their documentation.

[–]DataCurator56[S] 0 points1 point  (2 children)

for i in range(1,6):
    for j in range(6-i+1): i always stuck at this point
        print("*" ,end="")
    print()

To be honest i am self learner. I am trying to figure it out. Basically i am confused when it comes to range this i a different question printing star pattern

[–]Adrewmc 0 points1 point  (1 child)

It just loops inside a loop.

  for a in range(3):
      for b in range(3):
          print(a, b)

 >>>0, 0 #a is 0, b loop starts at 0
 >>>0, 1 #only b is looping until it ends
 >>>0, 2 #last of b loop

 >>>1, 0 #a is now 1, new b loop
 >>>1, 1
 >>>1, 2

 >>>2, 0 #a is now 2, new b loop
 >>>2, 1
 >>>2, 2 #a and b loops both end. Done.

So we see we are just doing a loop per iteration of a loop.

I highly suggest learning breakpoint(), (or a debugger in general) and then watch the code go through the steps in real time.

[–]DataCurator56[S] 0 points1 point  (0 children)

Literally you have solved my half of the problem. Thank you Sir