Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't sure why you put class here because the indent is not true and the way you use "<strong>method</strong>" is not like you want to use class. I have two code. One is using class and other is none.</p> <p>Firstly, assume that you really want to use class (and I think this best for your problem) :</p> <pre><code>class Generator: words = "" # you should put words and letters here. this will be attribute and both get_words() and sort_words() can see this letters = [] def get_words(self): words = raw_input("input the letters\n") letters = [] # i = 0 : no effect. it just local variable with method get_words def sort_words(self): i = 0 # must be assign value before use for word in words: letters.append(word) print letters[i] i = i + 1 </code></pre> <p>You should notice, when you use class, each method must have keyword <code>self</code>. Here is the test :</p> <pre><code>generator = Generator() generator.get_words() generator.sort_words() </code></pre> <p>If this is just <strong>normal function</strong>. your problem doesn't work because some reasons :</p> <p>1) <strong>words and letters might not be declare as global</strong>. So, when you assign value to those variable in get_words(), sort_words() doesn't see that. If you compile and no error, just some reason , you have assign those value SOMEWHERE in global (type name and assign value somewhere else, not in any function). But in case of that, when you use get_words(), those global still NOT update. So, no "print word" as you see, because "word" you use, doesn't update yet.</p> <p>2) i variable : must be assign value <strong>before use</strong>.</p> <p>So, here is the code that I have fixed:</p> <pre><code>def get_words(): global words global letters words = raw_input("input the letters\n") letters = [] # global i=0 : you can do that, but SHOULDN'T def sort_words(): i = 0 # i must be assign value before use for word in words: letters.append(word) print letters[i] i = i + 1 </code></pre> <p>and the test will be:</p> <pre><code>get_words() sort_words() </code></pre> <p>Hope this will help you :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload