Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, if/elif/else syntax split up by comments?

I wrote this function as part of a scrolling panel I was trying to make in pygame. The function is part of a class called slot, which is a part of the panel class, slots being the bars on the panel containing strings or data. this method draws the slots to the screen, drawing only a part of a slot if the screen in in between slots. The if/elif/else syntax is tripping me up:

    def mydraw(self,my_pygame,scrn,panel_x,panel_y,scroll_at,window_size,virtual_size):
        if((self.y<(scrollat+window_size))or((self.e_y)>scrollat)):
            onscreensy = (self.y-scrollat)+panel_y
            onscreeney = onscreensy + self.dim_y
            """the top case"""
            if(onscreensy<panel_y):
                onscrnwidth = (onscreensy+self.dim_y)-panel_y
                onbitmapwidth = self.dim_y-onscrnwidth
                holder = self.bitmap.subsurface(0,onbitmapwidth-1,self.dim_x,onbitmapwidth-1)
                scrn.blit(holder,(panel_x,panel_y))
            """end top case"""
            """the normal case"""
            elif(onscreeney<(panel_y+window_size)):  #### right here ####
                scrn.blit(self.bitmap,(panel_x,onscreensy))
            """end normal case"""
            """the bottom case"""
            else:
                onscrnwidth = (panel_y+window_size)-self.y
                onbitmapwidth = self.dim_y-onscrnwidth
                holder = self.bitmap.subsurface(0,0,self.dim_x,onbitmapwidth-1)
                scrn.blit(holder,(panel_x,onscreensy))
            """end bottom case"""
        if(self.has_next==True):
            self.next.mydraw(my_pygame,scrn,panel_x,panel_y,scroll_at,window_size,virtual_size)

error:

me$ python testscrolling3.py
  File "testscrolling3.py", line 164
    elif(onscreeney<(panel_y+window_size)):
       ^
SyntaxError: invalid syntax
like image 946
jason dancks Avatar asked Oct 24 '25 17:10

jason dancks


2 Answers

Those are not comments. They are strings, and are normal executable statements (even if they don't actually do anything).

Comments in Python start with #.

like image 65
Daniel Roseman Avatar answered Oct 26 '25 05:10

Daniel Roseman


Just as @Daniel_Roseman pointed out, they are strings. They can be kept in the function. Actually they can do something (rather than doing nothing), consider the following example do_sth3(), where the string follows the function declaration is the docstring.

But if we want to kept them in the function, the indentation has to be correct. Compares: do_sth1() to do_sth2()

In [53]:

def do_sth1():
    if 1==1:
        print 'Ok'
    '''Comment goes here'''
    else:
        print 'Not Ok'
  File "<ipython-input-53-fada2ba2e658>", line 5
    else:
       ^
SyntaxError: invalid syntax


In [54]:

def do_sth2():
    if 1==1:
        print 'Ok'
        '''Comment goes here'''
    else:
        print 'Not Ok'
In [55]:

def do_sth3():
    '''usage: just a test function'''
    print 'Ok'
In [56]:

do_sth3.__doc__
Out[56]:
'usage: just a test function'
like image 21
CT Zhu Avatar answered Oct 26 '25 05:10

CT Zhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!