Home > Software design >  Can anyone help me with this code to get the required output (see image)
Can anyone help me with this code to get the required output (see image)

Time:11-12

When I run the code I only get the "Working with numbers from 5 to 20" show up on the screen it doesn't show the prime numbers as shown in image pls help me.

 print("Working with numbers from 5 to 20")
 for num in range(5,20) :
 lim = int(num/2)
 k = 0
 for i in range(2, lim 1) :
 rem = num % i
 if rem == 0 :
 k = 1
 break
 else :
 continue
 if k == 1 :
 pass
 else :
 print(num," is a Prime Number ")

enter image description here

CodePudding user response:

In Python, it is required for indicating what block of code a statement belongs to, so in conclusion your code have indentation problem.

Code:

print("Working with numbers from 5 to 20")
for num in range(5,20) :
    lim = int(num/2)
    k = 0
    for i in range(2, lim 1) :
        rem = num % i
        if rem == 0 :
            k = 1
            break
        else :
            continue
    if k == 1 :
        pass
    else :
        print(num," is a Prime Number ")

Output:

Working with numbers from 5 to 20
5  is a Prime Number  
7  is a Prime Number  
11  is a Prime Number 
13  is a Prime Number 
17  is a Prime Number 
19  is a Prime Number

CodePudding user response:

Your code does not have a proper indentation.

Python indentation is a way of telling a Python interpreter that the group of statements belongs to a particular block of code.

Please refer to https://www.python.org/dev/peps/pep-0008/#indentation for more information about indentation.

I believe you would get an indentation error.

  • Related