0

I have an equation and want to sum it's results with different values. I enter a number using Number = input() and the equation takes numbers from the input according to an index. The wanted indices are from i = 1 to i = 4. The equation is : Number[i] + Number[i+1]*3 + i

So for example, if my input is Number = 879463, then firstly for i = 1 : Number[1] + Number[2]*3 + 1, which equals to 8 + 7*3 + 1 = 30

Then for i = 2 : Number[2] + Number[3]*3 + 2 .. and so on, until i = 4. At the end, sum the results and store in total.

Here is my code:

Number = input()

total = 0

def equation(i,Number):
    x = (Number[i] + Number[i+1]*3 + i)
    return x

for i in range(len(Number)):
    total += equation(i,Number)
    
print(total) 

For this code I get the error:

IndexError: string index out of range

Am I even in the right direction here ?

Programming Noob
  • 1,232
  • 3
  • 14
  • You might be mixing tabs and spaces in your indentation. Notice how the line `x = ...` is indented differently than the `return`? Also, you are trying to add strings with integers, and the `number[i+1]*3` part is different in your code vs your description. – tobias_k May 30 '22 at 21:38
  • 1
    in ```range``` the first ```i``` is 0 not 1 as you are suggesting, to get it to start at 1 you need to do ```range(1, len(Number))``` – Nin17 May 30 '22 at 21:44
  • @tobias_k Thanks, didn't notice those silly mistakes. I edited my code and error. – Programming Noob May 30 '22 at 21:50
  • @Nin17 Yes, thanks I edited it. Still giving me error though. – Programming Noob May 30 '22 at 21:51
  • 1
    Try changing the loop to `for i in range(len(Number) - 1):`, otherwise `i+1` will be out of ounds. – tobias_k May 30 '22 at 21:51
  • You're in the right direction, but you're making a bunch of different mistakes: A) If `i` is supposed to go to 4, then why are you using `len(Number)`? B) Python uses 0-indexing, which means you actually want `i` to go from 0 to 3. C) You never convert the digits from strings to integers. I'm voting to close the question because there are multiple unrelated problems. We're not here to do your debugging for you. You might benefit from [using a debugger](/q/4929251/4518341), or at least adding some prints like `print(f'{Number = }')` to check the values. – wjandrea May 30 '22 at 22:56

1 Answers1

1

# input is a string

number = input("Enter a number: ")

#  Two lists to store values, results and list of numbers

list_of_numbers = []
total = []

# List to store string values to integers

for i in number:
    list_of_numbers.append(int(i))
print(f'List = {list_of_numbers}')

# List to store values to total

for i in range(1, len(list_of_numbers)):
    result_equation = list_of_numbers[i - 1] + list_of_numbers[i] * 3 + i

    print(f'For i = {i},  {list_of_numbers[i - 1]} + {list_of_numbers[i]}*3 + {i} = {result_equation}')
    total.append(result_equation)

# Sum values
print(f'Total = {sum(total)}')

# Observation: Most of this code can  be erased as it is only used to print values on the screen.
Fabio
  • 26
  • 2