tags:

views:

165

answers:

8

For some reason this code will not seem to run in the interpreter. When I hit F5 nothing happens, not even the debugger seems to recognize it. I assume it has something to do with the class, as when removed the interpreter seems to recognize the rest of the code. Please tell me what I am doing wrong. Edit: I have restarted the interpreter multiple times, any other piece of code I try to load runs fine, just this one is having trouble.

edit: removed the initial print statement edit: fixed the indentation in the post code. edit: I have updated the code with fixes mentioned in the comments below; however, idle still does nothing when I try and run the code. I am using python 3.1.1 by the way.

class sorting_class:
    def __init__(self):
        self.globali = 0
        self.order = ['a', 'b', 'c', 'd']
        self.orderi = 0
        self.sortedlist = []
    def sort(self, array):
        carry, leave = []
        for arrayi in array:
            print ('run', arrayi)
            if self.order[self.orderi] == arrayi[self.globali]:
                carry.append(arrayi)
            else:
                if self.globali != 0:
                    leave.append(arrayi)
        return carry, leave
    def srt(self, array):
        globalii = 0
        carry, leave = my.sort(array)
        while len(self.sortedlist) != len(array):
            if len(carry) == 1:
                self.sortedlist.append(carry)
                arrayt = leave
                self.globali = 1
                self.orderi = 0
                carry, leave = my.sort(arrayt)
            elif len(carry) == 0:
                if len(leave) != 0:
                    arrayt = leave
                    self.globali = 1
                    self.orderi = 1
                    my.sort(arrayt)
                else:
                    self.arrayt 
                    globalii += 1
                    self.orderi = globalii
                    self.globali = 0
                    my.sort(arrayt)
                    self.orderi = 0
            else:
                arrayt = carry
                carry = []
                self.globali += 1
                carry, leave += my.sort(arrayt)
my = sorting_class()
x = ['ac', 'bc' ,'ab', 'da']
prin = my.srt(x)
print (prin)
+2  A: 

The source highlighting should give you a hint. You either need to switch to double quotes, or escape the ' in won't.

Change

print ('Why won't this work?')   

to

print ('Why won\'t this work?')   
Stefan Kendall
I actually added that after testing. The program still won't run even with the print statement removed.
Protean
This also works: "Why won't this work?"
inspectorG4dget
A: 

Seems that you have a problem with your indentation

...     my = sorting_class()
  File "<stdin>", line 45
    my = sorting_class()
                       ^
IndentationError: unindent does not match any outer indentation level
>>>     x = ['ac', 'bc' ,'ab', 'da']
  File "<stdin>", line 1
    x = ['ac', 'bc' ,'ab', 'da']
    ^
IndentationError: unexpected indent
>>>     my.srt(x)

once you get the indenting sorted you probably should consider having aprint statement somewhere

gnibbler
Thanks for the help, unfortunately that error is from me improperly posting the code and does not solve my problem. Ive fixed the indentation in the post now.
Protean
A: 

You're my = sorting_class() is defined inside the class. Take it out the class definition and you should be all set.

inspectorG4dget
That was a mistake in how I posted the code. I've fixed the formatting to reflect what It should have been.
Protean
A: 

You reference my within your class methods when they are clearly not in scope anywhere. I'm not sure what this should even be referring to.

Also, it's indented one level too far.

Stefan Kendall
+2  A: 

I got the following error message when running your code:

  File "sss.py", line 44
    carry, leave += my.sort(arrayt)
SyntaxError: illegal expression for augmented assignment

You should get similar error messages too. BTW, I am using python 2.6.4 for testing.

You can't append like this. You'll have to make some dummy variables, as far as I know, set them to my.sort(arrayt) and then append to carry and leave.
Justin Peel
+1  A: 

I wonder if you're just having trouble working with IDLE. You mentioned elsewhere that the debugger just says "None". When I select "Debugger" from the IDLE interpreter window Debug menu, it pops up another window with the title "Debug Control", and it says "None" in that window.

That window is just a control panel. You still have to run the program, by choosing "Run" from the "Run" menu of the window that has your code in it. That will make something appear in the "Debug Control" window, and it will un-gray the buttons in that window ("Go", "Step", and so on) so you can actually use the debugger to run or step through your program.


When I cut & paste your code into an IDLE window and use the Run/Check Module option, it reports an illegal expression on line 44:

                carry, leave += my.sort(arrayt)

which looks pretty peculiar. The LHS is a tuple; tuples are immutable, you can't use += to modify them. Looks like you meant a plain equals sign there, since the sort method returns a tuple.

                carry, leave = my.sort(arrayt)

ETA: Ah, Stefan & John are also correct. You're sometimes using the name you used for your instance variable (my) instead of self when calling one method of the class from inside another.

Vicki Laidler
What are you using to run it? When I try an tun the code nothing happens at all, Idle just stays idle.
Protean
Here's exactly what I did: - started IDLE - chose File/New Window to open up a non-interpreter window - in that window, I pasted in your code - that window has a "Run" menu, from which I chose "Check Module".It then saves the file, does the equivalent of importing the file which is the stage at which syntax errors are checked, and pops up with the error message.
Vicki Laidler
A: 
John Machin
I'm using 3.1.1. Even with your changes nothing happens, the debugger literally says 'None' after I try to run it. I'm having no problems running any other code I have written. Is there another program I could use other then Idle to run it?
Protean
@Protean: edit your question to say EXACTLY what you are doing, from starting IDLE ('try to run it' is NOT sufficient detail), and exactly what happens.
John Machin
@Protean: "None" is a special Python constant, and is returned by methods or functions that don't return anything else. Your methods don't return anything, they operate in place; and they don't print anything; so your program may now be working. Try putting in a *print "all done"* statement after the while loop in your srt method, and then try printing my.sorted_list at the very end, after you call my.srt().
Vicki Laidler
@Vicki: When I run it in both 3.1 and 2.6 in a Command Prompt window, it executes `print ('run', arrayi)` several times before the IndexError. Hmmm maybe he's fixed all his bugs (!) AND removed the print ...
John Machin
@John: Ah, I missed those print statements in my quick skim.
Vicki Laidler
A: 

There are too many errors in your code. Write down in plain English your requirements and start from scratch.

What are you trying to do? Is it:

x = ['ac', 'bc' ,'ab', 'da']
order = ['d', 'b', 'c', 'a']
assert len(order) == len(x)
x = [item for o, item in sorted(zip(order, x))]
print x
# -> ['da', 'bc', 'ab', 'ac']
J.F. Sebastian