-3

I'm trying to make an "x" character move through a list, from one index to the other one and then returning over and over again. I want to know if it's possible to make it constant, so that in the Python shell I can see only one list with a moving x. I've tried doing it with this code, but I'm pretty sure there must be other way to do it. Sorry for my identation, I'm new and don't understand much.
Would love your help, thanks.


def vec ():


    vec=[]
    z=0
    x="x"
    e=" "
    y=1

    while (x=="x"):
        while (y<=98):

            vec[z]= e
            z=z+1
            vec[y]=x
            y=y+1

   vec[y]= e
   w=98

   m=99

   while (m>0):
       vec[m]= e
       vec[w]= x
       w=w-1
       m=m-1
Sophie
  • 1
  • 1
  • Terminology note: the term vector is not commonly used in Python, rather, those are called `list` objects. Also, *always* use a generic python tag, and only use a specific version if your problem or question is version specific, but always include the generic tag. – juanpa.arrivillaga Oct 20 '17 at 16:51
  • 4
    Possible duplicate of [IndexError: list assignment index out of range](https://stackoverflow.com/questions/5653533/indexerror-list-assignment-index-out-of-range) – juanpa.arrivillaga Oct 20 '17 at 16:53
  • 1
    I would suggest if you can indent the code correctly otherwise its not readable. whitespace is important in python. Also, explain what you are trying to do in the code. – Deepak Oct 20 '17 at 17:34
  • Hi, I´m trying to move the "x" trough the list, so it seems like it is running form one side of the list and then returning. It's some kind of visual effect. If there's another way to do it, I would appreciate your help. – Sophie Oct 20 '17 at 17:49

1 Answers1

0

You are getting the index out of range because your "vec" list has zero elements. You need to initialize it in some way. One way is to do something like:

vec = [e] * 100

This will create a list of 100 elements, each initialized to the value of e.

John Anderson
  • 35,991
  • 4
  • 13
  • 36