There are a couple of issues with your code.
- Your while loop never executes because
z
is 1 andsk
is 10, so they are not equal. sk % x
.x
is a range object, you don’t want to use the modulus on that.if x is x % 2 != 0
this bit is wrong. You probably want to remove thex is
- You don’t need
z
, if you loop over the range object you already have the numbers from 1 to 10. Then you can also remove the nested if.
Here is a simplified version of your code that works, using a for loop as someone suggested in the comments.
sk = int(input("Enter number: "))
for x in range(1, sk + 1):
if (sk % x) == 0:
print(sk, "is divisible by", x)
else:
print(sk, "is not divisible by", x)
Gives
Enter number: 10
(10, 'is divisible by', 1)
(10, 'is divisible by', 2)
(10, 'is not divisible by', 3)
(10, 'is not divisible by', 4)
(10, 'is divisible by', 5)
(10, 'is not divisible by', 6)
(10, 'is not divisible by', 7)
(10, 'is not divisible by', 8)
(10, 'is not divisible by', 9)
(10, 'is divisible by', 10)
All you have to do is loop over the range 1 to sk
, and check if sk
is divisible by that number by doing if (sk % x) == 0:
Edit: actually that code doesn’t check if the number is divisible by 2 3 or 5. Here is a snippet that does
sk = int(input("Enter number: "))
for x in range(1, sk + 1):
if x % 2 == 0 or x % 3 == 0 or x % 5 == 0:
print(x, "is divisible by 2 3 or 5")
else:
print(x, "is not divisible by 2 3 or 5")
So instead of checking if (sk % x) == 0
, we now check if x % 2 == 0 or x % 3 == 0 or x % 5 == 0
The output is now
Enter number: 10
(1, 'is not divisible by 2 3 or 5')
(2, 'is divisible by 2 3 or 5')
(3, 'is divisible by 2 3 or 5')
(4, 'is divisible by 2 3 or 5')
(5, 'is divisible by 2 3 or 5')
(6, 'is divisible by 2 3 or 5')
(7, 'is not divisible by 2 3 or 5')
(8, 'is divisible by 2 3 or 5')
(9, 'is divisible by 2 3 or 5')
(10, 'is divisible by 2 3 or 5')
12
solved How to find all numbers between 1 and N that are not divisible by 2, 3 and 5 [closed]