The goal of the problem is to find how many times a sequence created from a string would be divisible by 5.
Sequences for number 125 would be = 1, 2, 5, 12, 25, 125
The number is a string.
Code might have a few bugs besides that.
However, the main issue is that I cannot go from a string (for instance '12') to an integer (12). The part of the code that produces an error is between #### lines. If you run the code, the error message would be
ValueError: invalid literal for int() with base 10: ''
Could you please write why this error occurs and what would be the workaround?
def DivByFive(number):
answer = 0
leng = len(number)
for sizen in range(leng):
if sizen != leng:
i = 0
j = i + sizen
while True:
if j > leng:
break
###########################################
# Problem is in here with "number[i:j]"
numb = number[i:j]
if int(numb) % 5 == 0:
answer += 1
# ValueError: invalid literal for int() with base 10: ''
###########################################
i += 1
j += 1
else: # if entire number
if int(number) % 5 == 0:
answer += 1
return answer
number = '125'
result = DivByFive(number)
print("Result is", result)