IMMEDIATE PROBLEM
You increment your index
value before you use it; this means that on the final iteration, it’s 1 too large for the final operation. Switch the bottom two lines of your loop:
index = 0
while index < 7:
print(arr[index])
totalNum = int(num[index]) * arr[index]
index += 1
Even better, this should be a for
loop, not a while
:
for index in range(7):
print(arr[index])
totalNum = int(num[index]) * arr[index]
Python will guard your value better.
AFTER THIS REPAIR:
8
7
6
5
4
3
2
14
['0317847', -8]
Now, your problems include
- You keep only the last weighted amount in
totalNum
. You need to make a running sum for the entire group. - You have the final subtraction backwards.
I assume you can fix things from here.
Remember that you still have to handle the X
case.
2
solved ISSN Calculator in Python