-4

please consider the following code:

def getString(absA, y):
    i = 0
    t = list(range(100))
    while absA > 0:
        t[i] = absA % y
        absA = absA / y
        i = i + 1  
    for j in range(len(t)):
        if t[j] == '10':
            t[j] = 'A'
        elif t[j] == '11':
            t[j] = 'B'
        elif t[j] == '12':
            t[j] = 'C'
        elif t[j] == '13':
            t[j] = 'D'
        elif t[j] == '14':
            t[j] = 'E'
        elif t[j] == '15':
            t[j] = 'F'
    return t

bitString = getString(abs(eval(a)), eval(b))

I guess it's some simple things I don't know.Why does this list go out of range?

2 Answers2

0

You go out of range because this loop in particular never checks if i might exceed the bounds of the list t.

while absA > 0:
    t[i] = absA % y
    absA = absA / y
    i = i + 1  

Code Note:

Your comparisons of the form:

if t[j] == '10': ...

will not do what you hope they will do. The element t[j] is a number not a string.

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
0

while absA > 0: is an infinite loop and after too many rotations returns the error you get. It's a logical error.

Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23