Results 1 to 6 of 6

Thread: Python Syntax Error

  1. #1
    Join Date
    Apr 2010
    Beans
    20

    Python Syntax Error

    Running from the command line I get this error. The thing is, the program I'm running is 33 lines long. When I try to run it in IDLE, it gives the same error and highlights the last space after the text on the last line of the program. Thanks for any help in advance.


    The error:
    Code:
    :~$ python v.py
      File "v.py", line 34
        
                                      ^
    SyntaxError: invalid syntax

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Python Syntax Error

    Try posting the code - in code tags - so we can actually help.

    It could be something simple like a missing bracket.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Apr 2010
    Beans
    20

    Re: Python Syntax Error

    Here's the code:
    Code:
    import pygame
    import os.path, sys
    
    pygame.init()
    mixer = pygame.mixer
    
    main_dir = os.path.split(os.path.abspath(__file__))[0]
    
    def main(file_path=None):
    
        if file_path is None:
            file_path = os.path.join(main_dir,
                                     'data',
                                     'mp3.ogg')
        mixer.init(11025) #raises exception on fail   
        sound = mixer.Sound(file_path)
        print ('Playing Sound...')
        channel = sound.play()
        playing = True
        while playing:
            track_length = sound.get_length()
            w = 300
            h = 70
        rect_w = 50
            screen = pygame.display.set_mode((w,h))
            pygame.draw.rect(screen, (0, 255, 0), (10, 10, track_length, rect_w))
            drawing = True
        time_passed = pygame.time.get_ticks()
        pygame.draw.rect(screen, (225, 0, 0), (15, 15, time_passed, (rect_w - 5))
    
    
    if __name__ == '__main__':
        if len(sys.argv) > 1:
            main(sys.argv[1])
        else:
            main()
    To add to what I wrote earlier, IDLE takes issue with the colon after '__main__' but if I remove the last section of code entirely then the issue I described above begins to happen. Thanks for any help, I'm teaching myself python and this is just an experiment I wanted to try (so if you spot any poor coding practices etc feel free to point them out ). Also, the number of lines is different here because I removed the comments.

  4. #4
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python Syntax Error

    The indentation of the "rect_w = 50" line is wrong, assuming that it's meant to be part of the "while playing" loop. Either way, that line is then followed by three indented lines, which are orphans (presumably meant to be in the "while playing" block).

    You're also missing a closing parens on the last "pygame.draw.rect" line. I think that's the source of the error (although the other thing is also an error).

  5. #5
    Join Date
    Apr 2010
    Beans
    20

    Re: Python Syntax Error

    Thanks! That fixed it.

    You were right, the missing parenthesis were the problem (so many brackets!)
    Feel a bit silly missing the formatting error at 'rect_w' but it seems it was due to using the tab key to indent that line which is why I never saw it, but then that's an even sillier mistake... Oh well

    Thanks very much for your help.

  6. #6
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Python Syntax Error

    Quote Originally Posted by skaarr View Post
    Thanks! That fixed it.
    Feel a bit silly missing the formatting error at 'rect_w' but it seems it was due to using the tab key to indent that line which is why I never saw it, but then that's an even sillier mistake... Oh well

    Thanks very much for your help.
    Which ever editor you are using configure it to convert Tabs to spaces - that way you can still use the Tab key but the indentation still works.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •