Task 13 - Weighted sum of digits

Back to Problem Solutions forum

PandaProgram     2015-12-01 22:30:21

I'm not sure where I'm going wront. It keeps saying that my index is out of range. To the best of my knowledge, the variable tick is 0 at the beginning. I incriment it every time the for loop exits. I'm just stuck....

            count = int(raw_input())
            pre_count = raw_input().split()
            tick = 0
            total = 0

            while tick < (count + 1):
                tock = 1
                summ = 0
                for i in str(pre_count[tick]):
                    i = int(i)
                    summ = (i * tock) + summ
                    tock = tock + 1
                tick = tick + 1
                print summ,

Help would be greatly appreciated!

Rodion (admin)     2015-12-02 05:23:36
User avatar

Hi there! Thanks for your message!

Will you allow me not to point directly to the cause of the trouble, but try to hint how you yourself can find it out?

Insert the line print 'Tick=' + str(tick) at the very beginning of the loop, before tock = 1 for example, and use some input data with small value of count, for example 1 or 3. See how many times the loop is executed. Does anything seems correct? Feel free to ask more if the hint is not enough. I'll gladly help in debugging!

Christopher Matthews     2015-12-02 19:04:45

I would suggest using names for your variables that match what the variable is used for. Names such as "tick" and "tock" remind me of clocks, and do not help me understand the code at all, and therefore I have to look deeper and spend that much more time trying to figure out what is going on. Using good naming conventions go a long way in enabling other programmers to quickly analyze what your code is trying to do, and thus can help you debug your code much faster :)

-- Christopher P. Matthews

PandaProgram     2015-12-02 19:24:59

Thank you guys for the help. I took out the '+1' in my while loop and also changed the names of my variables so they're easier for others to understand. Thanks for the advice, Admin, and also you, Christopher. =]

Please login and solve 5 problems to be able to post at forum