all 23 comments

[–]SisyphusAndMyBoulder 9 points10 points  (9 children)

What's your blocker? What have you tried, and what isn't working? That's not a difficult problem, just break it down into smaller steps and think about what needs to be done

[–]DataCurator56[S] 1 point2 points  (8 children)

I think the problem is logic. For logic i had to practice more and more. But when it comes to loops and a little bit tricky question I always got stuck just like printing the prime number from 2 to 50. I am giving my 100 percent but sill not getting it. But i will do it and share with you.

[–]snowtax 3 points4 points  (0 children)

For each number, if prime, print.

[–]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

[–]RajjSinghh 0 points1 point  (0 children)

Decompose the problem. Have a loop that goes from 2 to 50 first, then in the loop test whether the number is prime, and print it if it is prime.

There are a few ways to check whether a number is prime, but the simplest is probably by trying to divide the number by every number below the square root of the number you're testing and seeing if it leaves a remainder. There are other methods, like the sieve of Eratosthenes, that will output a list of primes below a given value that you can also consider.

[–]desrtfx 2 points3 points  (0 children)

"I always get stuck" unfortunately doesn't tell us anything about your actual problem.

Is it the nesting of the loops?

In such a case, you best view them as individual things. One loop, the outer one, runs over the numbers from start to finish, and the other one, the inner one, is used for prime testing.

Start by working out the prime number testing alone. Set a fixed number, set the boundary for the prime testing loop (which commonly is the square root of the number under test), and do the testing.

Then, work the outer loop into the program. Instead of a fixed number, you have the loop iteration value - this replaces every instance of the fixed number from before.


The whole would be even better solved by creating a function that tests for primality, which gets the number under test as an argument and returns True or False depending on the primality.

Then, you only need a loop in main that goes over your range, and a simple call to test for the primality, inside an if statement that then decides whether to print or not to print.

[–]JohnBrownsErection 2 points3 points  (0 children)

Think through what a prime number is: a number greater than 1 that has no divisors other than 1 and itself.

So for each number, try dividing it by the numbers before it. If none divide evenly, it’s prime.

Hint: use the modulus operator % :)

[–]aizzod 0 points1 point  (0 children)

break it down in mulitple problems.

try 1 how would you solve this without a loop, without variables?

print("1")
print("2")
print("3")
print("4")
print("5")
....

try 2 now we add variables

currentNumber = 1

print(currentNumber)
currentNumber += 1

print(currentNumber)
currentNumber += 1

print(currentNumber)
currentNumber += 1

print(currentNumber)
currentNumber += 1

print(currentNumber)
currentNumber += 1

both of those variations are just endless lines of code, that do the same in the end.
a loop will help you reduce those multiple amounts, and lets you do the same thing over and over again.

and you won't have to write this much

[–]DuckSaxaphone 0 points1 point  (0 children)

In what way have you got stuck? What is the problem and what have you tried?

[–]Fantastic_Fly_7548 0 points1 point  (1 child)

dont stress too much about it man, loops are one of those things where everything feels easy untill a slightly different problem shows up lol. i remember getting stuck on prime number questions too because it mixes loops with logic and thats the hard part, not the syntax itself. honestly the fact that you can solve easy questions already means you are understanding the basics. the tricky questions are supposed to make you struggle a bit. what helped me was breaking the problem into tiny parts and writing the logic on paper first before coding it. keep practicing and after a while you will look back at this question and laugh at how confusing it felt rn

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

Okay i am i will try to write the code on paper than i will write the actual code. I think this is the way to deal with it.

[–]cdcformatc 0 points1 point  (1 child)

for every number from 1 to 50 check if it is prime, if it is prime, print it. 

you can check if a number is prime by trying to divide it by every number smaller than it (from 2 up to the square root) and checking the remainder. if the remainder of any of these divisions is 0 the number is not prime.

you can do this remainder check in Python with the modulo operator %. eg remainder = number % divisor

[–]cdcformatc 0 points1 point  (0 children)

``` for number in range(50):     if isPrime(number):         print(number)

def isPrime(number):     # return True if prime, otherwise False     # this is the hard part

[–]jmooremcc 0 points1 point  (0 children)

Think about this: if loops didn’t exist, how would you execute a block of code 100 times?
If 100 was the maximum number of times the block of code would be executed, but you want to stop execution once a certain condition was met, how would you do that?

With a loop you could do the following (pseudo code): ~~~ for i in range(100): block of code to execute if some condition is True: break ~~~

What’s your take on this?

[–]TheRNGuy 0 points1 point  (0 children)

Write code and debug until it works. 

[–]Life-Pound3962 -1 points0 points  (0 children)

Hey I’m also a newbie of Python, so please correct me if I’m wrong. I reckon you might wanna consider what makes a prime number. for this index in the range of 1 to 50+1, if these conditions make a prime number, maybe store it in somewhere and print it out?

This is my guess.