make a range of numbers from 1 to 101 and see that it is divisible by 5 using while loops
Question:
I want to make a program that checks a range of numbers from 1 to 101 using the while
loop.
My code doesn’t have any errors and is scanning all of the numbers in the list but the code only says, "the number is divisible by 5" 101 times.
Here’s the code:
skip = range(0 , 101, 5)
while True:
for i in range(101):
if i == skip:
print("the number is divisible by 5")
elif i != skip:
print("the number is not divisible by 5")
break
Answers:
Right now you are comparing i
(type: integer) to skip
(type: range). I provide an explanation for two options, change your if-statement or change to modulo calculation.
Option 1: Modulo
skip = range(0 , 101, 5)
for i in range(101):
if i % 5 == 0:
print("the number is divisible by 5")
elif i % 5 != 0:
print("the number is not divisible by 5")
Given two positive numbers a and n, a modulo n (often abbreviated as a mod n or as a % n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. The modulo operation is to be distinguished from the symbol mod, which refers to the modulus1 (or divisor) one is operating from.
https://en.wikipedia.org/wiki/Modulo_operation
if i % 5 == 0:
This line would calculate in this order:
if 0 % 5 == 0
if 1 % 5 == 0 (1%5 = 1)
if 2 % 5 == 0 (2%5 = 2)
.
.
if 5 % 5 == 0 (5%5 = 0)
if 6 & 5 == 0 (6%5 = 1)
.
.
if 9 % 5 == 0 (9%5 = 4)
if 10 % 5 == 0 (10%5 = 0)
if 11 % 5 == 0 (11%5 = 1)
.
etc
Option 2: use of skip
Instead of just comparing to the object itself, you change you if-statement to if i in skip:
to see if i
is within skip.
skip = range(0 , 101, 5)
for i in range(101):
if i in skip:
print("the number is divisible by 5")
elif i not in skip:
print("the number is not divisible by 5")
Also, I removed you while True
, since it is redundant when you already have the for-loop. the while loop will only run once since you end it after the for-loop has executed.
for i in range(101):
if i % 5 == 0:
print("the number "+str(i)+" is divisible by 5")
elif i % 5 != 0:
print("the number "+str(i)+" is not divisible by 5")
If you want to do it with while loop.
i = 0
while i <= 101:
if i % 5 == 0:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")
i += 1
But it’s better to use range function
for i in range(101):
if i % 5 == 0:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")
you can try this
i=1
while i in range(1 , 102):
if i%5==0:
print("the number",i,"is divisible by 5")
else:
print("the number",i,"is not divisible by 5")
i=i+1
and remove the else print statement if you want to print not divisible by 5
I’ll forge an answer from comments by 9769953
Solution
The problem you’re experiencing is caused by type mismatch. If you print types of what you compare, you’ll get the following:
>>> type(i)
<class 'int'>
>>> type(skip)
<class 'range'>
This comparison will always be false, because an int
can never equal a range
. What you want to use instead, is in
keyword:
skip = range(0 , 101, 5)
while True:
for i in range(101):
if i in skip:
print("the number is divisible by 5")
elif i not in skip:
print("the number is not divisible by 5")
break
This will say the number is divisible always when i
belongs to skip
range.
Optional improvements
Moreover, you can make a few improvements of the code.
while True: ....... break
will be executed once anyway, this loop can be omitted in its entirety
elif i not in skip
can be turned into else
, as this is the exact opposite of used if
condition
skip = range(0 , 101, 5)
for i in range(101):
if i in skip:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")
Lastly, you can use modulo operation instead of skip
:
for i in range(101):
if i % 5 == 0:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")
I want to make a program that checks a range of numbers from 1 to 101 using the while
loop.
My code doesn’t have any errors and is scanning all of the numbers in the list but the code only says, "the number is divisible by 5" 101 times.
Here’s the code:
skip = range(0 , 101, 5)
while True:
for i in range(101):
if i == skip:
print("the number is divisible by 5")
elif i != skip:
print("the number is not divisible by 5")
break
Right now you are comparing i
(type: integer) to skip
(type: range). I provide an explanation for two options, change your if-statement or change to modulo calculation.
Option 1: Modulo
skip = range(0 , 101, 5)
for i in range(101):
if i % 5 == 0:
print("the number is divisible by 5")
elif i % 5 != 0:
print("the number is not divisible by 5")
Given two positive numbers a and n, a modulo n (often abbreviated as a mod n or as a % n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. The modulo operation is to be distinguished from the symbol mod, which refers to the modulus1 (or divisor) one is operating from.
https://en.wikipedia.org/wiki/Modulo_operation
if i % 5 == 0:
This line would calculate in this order:
if 0 % 5 == 0
if 1 % 5 == 0 (1%5 = 1)
if 2 % 5 == 0 (2%5 = 2)
.
.
if 5 % 5 == 0 (5%5 = 0)
if 6 & 5 == 0 (6%5 = 1)
.
.
if 9 % 5 == 0 (9%5 = 4)
if 10 % 5 == 0 (10%5 = 0)
if 11 % 5 == 0 (11%5 = 1)
.
etc
Option 2: use of skip
Instead of just comparing to the object itself, you change you if-statement to if i in skip:
to see if i
is within skip.
skip = range(0 , 101, 5)
for i in range(101):
if i in skip:
print("the number is divisible by 5")
elif i not in skip:
print("the number is not divisible by 5")
Also, I removed you while True
, since it is redundant when you already have the for-loop. the while loop will only run once since you end it after the for-loop has executed.
for i in range(101):
if i % 5 == 0:
print("the number "+str(i)+" is divisible by 5")
elif i % 5 != 0:
print("the number "+str(i)+" is not divisible by 5")
If you want to do it with while loop.
i = 0
while i <= 101:
if i % 5 == 0:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")
i += 1
But it’s better to use range function
for i in range(101):
if i % 5 == 0:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")
you can try this
i=1
while i in range(1 , 102):
if i%5==0:
print("the number",i,"is divisible by 5")
else:
print("the number",i,"is not divisible by 5")
i=i+1
and remove the else print statement if you want to print not divisible by 5
I’ll forge an answer from comments by 9769953
Solution
The problem you’re experiencing is caused by type mismatch. If you print types of what you compare, you’ll get the following:
>>> type(i)
<class 'int'>
>>> type(skip)
<class 'range'>
This comparison will always be false, because an int
can never equal a range
. What you want to use instead, is in
keyword:
skip = range(0 , 101, 5)
while True:
for i in range(101):
if i in skip:
print("the number is divisible by 5")
elif i not in skip:
print("the number is not divisible by 5")
break
This will say the number is divisible always when i
belongs to skip
range.
Optional improvements
Moreover, you can make a few improvements of the code.
while True: ....... break
will be executed once anyway, this loop can be omitted in its entiretyelif i not in skip
can be turned intoelse
, as this is the exact opposite of usedif
condition
skip = range(0 , 101, 5)
for i in range(101):
if i in skip:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")
Lastly, you can use modulo operation instead of skip
:
for i in range(101):
if i % 5 == 0:
print("the number is divisible by 5")
else:
print("the number is not divisible by 5")