-3

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)

Aleksei
  • 77
  • 8
  • I think that should be `numb=number[i,j]`, not `number[i:j]`? – Bart Nov 02 '21 at 15:15
  • I don't think so @Bart – President James K. Polk Nov 02 '21 at 15:20
  • 1
    Unfortunately we don't know what `number` is, so @alex, perhaps you can turn your code into a minimal reproducible/working example? See: https://stackoverflow.com/help/minimal-reproducible-example – Bart Nov 02 '21 at 15:24
  • Your loop assigns to `sizen` the values 0, 1, ..., leng - 1, so testing if `sizen != leng` is a waste and makes the code harder to read. Similarly, the while loop with the if following can be simplified to `while j <= leng:`. – President James K. Polk Nov 02 '21 at 15:25
  • The very first time through the code, `i` and `j` are both zero, so you're calling `int()` on `number[0:0]` which is an empty string. Just as the error message says. – John Gordon Nov 02 '21 at 15:28
  • Will minimize the code in a few minutes – Aleksei Nov 02 '21 at 15:28

3 Answers3

0

You first start with empty string : length must start at 1 not 0

Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
  • Indeed my number[0:0] (aka the first go through the for-loop) caused the error. @JohnGordon you were correct I am putting the solution below. – Aleksei Nov 02 '21 at 15:57
0

You should start your loop on the size at 1, for sizen in range(leng) will go from zero to leng-1. A length of zero is what causes your program to attempt converting an empty string to a number.

Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

Excuse me for the print-statements. Here is the workable code with the suggestions from @PresidentJames.

A big thank-you to @PtitXav and @JohnGordon for spotting the error.

def DivByFive(number):
    answer = 0
    leng = len(number)
    

    for len_of_sequence in range(1,leng):
      i = 0
      j = i + len_of_sequence
      while j <= leng:

          if i == j and j < leng:
            if int(number[j]) % 5 == 0:
              answer += 1
          else:
            numb = number[i:j] # <- error was here
            print(f'\nThis is {len_of_sequence} iteration with number[i:j] = {numb}')
            print(f'int(numb) = {int(numb)}')

            
            if int(numb) % 5 == 0:
                answer += 1
          i += 1
          j += 1

    # end of for loop


# if entire number
    print(f'\nThis is {len_of_sequence+1} iteration with number = {number}')
    print(f'int(number) = {int(number)}')
    if int(number) % 5 == 0:
        answer += 1

    return answer

number = '125'
sequences = DivByFive(number)
print(f"\nThere are {sequences} seqeunces that are divisible by 5 in the number '{number}'")

Output of the code

This is 1 iteration with number[i:j] = 1
int(numb) = 1

This is 1 iteration with number[i:j] = 2
int(numb) = 2

This is 1 iteration with number[i:j] = 5
int(numb) = 5

This is 2 iteration with number[i:j] = 12
int(numb) = 12

This is 2 iteration with number[i:j] = 25
int(numb) = 25

This is 3 iteration with number = 125
int(number) = 125

There are 3 seqeunces that are divisible by 5 in the number '125'
Aleksei
  • 77
  • 8