0

I am trying figure out if I'm having an issue with my classes, or calling variables/lists from another function.

Below is a function I have being called from a button I press(inside a class that is my main window):

def analyzeRNA(self):
        self.p = self.textbox.get("1.0", END)
        protein_assignment(self.p)
        self.data = primary_structure
        self.databox.insert(END, self.data)

It reads from my textbox just fine. It performs my function protein_assignment on it and prints the results to the python shell as in the function does. It prints a list I make in protein_assignment that function called primary_structure. Now, how do put this information into my databox (which is also a "textbox" so to speak)

My error:

line 86, in analyzeRNA self.data = primary_structure

NameError: global name 'primary_structure' is not defined

Protein assignment outline:

def protein_assignment(sequence):
    x = 0
    primary_structure = []
    single_letter_assignment = []
    three_letter_code = []
    chemical_properties = []
    while True:
        blah, blah, blah adding amino acids to the list, running through sequence
        "if nothing else in sequence, print lists and break"
        return primary_structure #(Not sure if this is needed)
    return primary_structure #(Not sure what I'm doing here either)

If more is needed, glad to help. Running off to work right now. Will respond later. Please and thank you!

Kingman
  • 3
  • 1
  • Possible duplicate of [Calling variable defined inside one function from another function](http://stackoverflow.com/questions/10139866/calling-variable-defined-inside-one-function-from-another-function) – LinkBerest Feb 24 '16 at 18:46
  • in `protein_assignment` you return `primary_structure` but it is not used from the call. – Tadhg McDonald-Jensen Feb 24 '16 at 19:38
  • @JGreenwall, this isn't a dup because @Kingman clearly already is using attributes in a class, the confusion seems to be what `return` does. – Tadhg McDonald-Jensen Feb 24 '16 at 19:44
  • @Kingman to loop over each character of a string or each element of a list you can use a `for` loop instead of a `while` loop: `for thing in sequence:#print(thing)` – Tadhg McDonald-Jensen Feb 24 '16 at 19:49

1 Answers1

0

primary_structure is returned by protein_assignment so to keep a reference to it in analyzeRNA you just need to do this:

def analyzeRNA(self):
        self.p = self.textbox.get("1.0", END)
        returned_value = protein_assignment(self.p)
        self.data = returned_value
        self.databox.insert(END, self.data)

or since you are just setting it to self.data anyway:

def analyzeRNA(self):
        self.p = self.textbox.get("1.0", END)
        self.data = protein_assignment(self.p)
        self.databox.insert(END, self.data)
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59