Implementing raw input in a method
<p>I have the following class below but struggling to make the script work as intended. </p>

<pre><code>class firstClass:
   def test(self):
      enter = raw_input()
      books = enter.split()
      books = []
      for index in range(len(books)):
        print 'Current Books :', books[index]

mybooks = firstClass()
mybooks.test()
</code></pre>

<p>My end goal is to have a situation where if i enter <code>book1</code>, <code>book2</code> <code>book3</code> and so forth, i get a result like so ( I dont want to limit it to just three books)</p>

<pre><code>Current Books, book1
Current Books, book2
Current Books, book3
</code></pre>

<p>You are setting <code>books</code> properly with the line <code>books = enter.split()</code>. The next line <code>books = []</code> <em>overwrites</em> this value with an empty array, so the loop never happens. </p>

<p>Removing that one line, it works as expected:</p>

<pre><code>&gt;&gt;&gt; class firstClass:
...    def test(self):
...       enter = raw_input()
...       books = enter.split()
...       for index in range(len(books)):
...         print 'Current Books :', books[index]
... 
&gt;&gt;&gt; mybooks = firstClass()
&gt;&gt;&gt; mybooks.test()
book1 book2 book3
Current Books : book1
Current Books : book2
Current Books : book3
</code></pre>

<p>As you probably know, <code>split()</code> without any arguments will split on whitespace, so this assumes that the input is entered with spaces in between. You will have to specify another delimiter (like a comma) explicitly if that is the case.</p>

<p>As another note, <code>index for range(len(arr))</code> is an anti-pattern in Python that should almost always be avoided. You for loop can be rewritten this way for readability:</p>

<pre><code>for book in books:
    print 'Current Books :', book
</code></pre>

python python-2.7