-1

I´m having some troubles on my code, that implements a function that verifies the lenght of a string and return that string with or without spaces.

If String b >= 15 return b

If strinf < 15 return "number os spaces until len(b)2 + string b

But I get a IndexError: list index out of range Error. I cannot figure out where or why.

Traceback:

============= RESTART: C:\Users\pbo\Desktop\Trab_04.py =============

ascii_letters = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ Traceback (most recent call last):

File "C:\Users\pbo\Desktop\Trab_04.py", line 30, in o2.append(a(o1[e]))

IndexError: list index out of range

============= ============= ============= ============= =============

Thanks a lot for any possible help.

  from random import seed
  from random import randint
  from random import choice
  from string import ascii_letters

  seed(930)

  def a(b):

    if(len(b)<15) :
        s = "";
        l = len(b)
        while(len(s) + len(b) < 15) :
            s += " "
        s += b
        return s
    else: 
        return b


 print("ascii_letters = " + ascii_letters)
 o1 = []
 for e in range(9622):
         k = randint(1, 25)
         s = ""
 for l in range(k):
         s = s + choice(ascii_letters)
 o1.append(s)
 o2 = []
 for e in range(9622):
         o2.append(a(o1[e]))
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 for e in range(9):
         print(o1[e] + "|")
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 for e in range(9):
         print(o2[e] + "|")


 x1 = ""
 x2 = "a"
 x3 = "abc"
 x4 = "abcdefghijklmnopqrstuvwxyz"
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 print(x1 + "|")
 print(x2 + "|")
 print(x3 + "|")
 print(x4 + "|")
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 print(a(x1) + "|")
 print(a(x2) + "|")
 print(a(x3) + "|")
 print(a(x4) + "|")
Paulo
  • 331
  • 2
  • 11
  • Please share the full traceback – Maurice Meyer Nov 13 '19 at 17:59
  • `o1` only has one element in it (`s`), so any index > 0 will raise that error – C.Nivs Nov 13 '19 at 18:04
  • Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Posting your entire program for an untraced 3-line makes this hard to read for future users. – Prune Nov 13 '19 at 18:10

2 Answers2

1

The problem is that your list o1 only has one element in it. Snipping out all of the rest of the code, you do this:

s = "some string"
o1 = []
o2 = []

# now has one element
o1.append(s)

for i in range(9622):
    # when i > 0, IndexError will be raised
    state = o1[i]
    o2.append(a(state))   

This happens because you are iterating over a range that has no relationship to the size/contents of o1. To fix this, you might consider:

s = "some string"
o1 = []
o2 = []

o1.append(s)
for element in o1:
    o2.append(a(element))

The reason being is that now you are iterating over o1 directly, rather than assuming it will always have 9622 elements in it, which it doesn't in this case.

Also, upon further inspection, it looks like your a function is just trying to pad a string with spaces out to 15 characters. Fortunately, there's a builtin for this:

x = "some string"

x.rjust(15)
'    some string'

Where rjust will left-insert spaces to create a string of the given length

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
1

It looks like your indentation is incorrect (see also What is Python Whitespace and how does it work?).

If you change the following:

o1 = []
for e in range(9622):
    k = randint(1, 25)
    s = ""
for l in range(k):
    s = s + choice(ascii_letters)
o1.append(s)

to:

o1 = []
for e in range(9622):
    k = randint(1, 25)
    s = ""
    for l in range(k):
        s = s + choice(ascii_letters)
        o1.append(s)

Then, your code appears to work.

rkersh
  • 4,447
  • 2
  • 22
  • 31