Syntax errors in code I copied from help site

This is the place for queries that don't fit in any of the other categories.

Syntax errors in code I copied from help site

Postby jdubya » Wed Apr 16, 2014 5:43 am

Hello all. First post. I just executed my first Hello World by Python in a terminal in Linux Mint. I copied a bubble sort implementation from a help site but I get errors trying to execute it. Does this code have errors or have I done something wrong?
Code: Select all
def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp

alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)

Here are the errors:
./PythonBubbleEx.py: line 1: syntax error near unexpected token `('
./PythonBubbleEx.py: line 1: `def bubbleSort(alist):'
Last edited by Mekire on Wed Apr 16, 2014 6:14 am, edited 1 time in total.
Reason: First post lock.
jdubya
 
Posts: 4
Joined: Wed Apr 16, 2014 5:10 am

Re: Syntax errors in code I copied from help site

Postby Mekire » Wed Apr 16, 2014 6:22 am

I find no syntax errors in the above program.
It prints this, as expected:
Code: Select all
[17, 20, 26, 31, 44, 54, 55, 77, 93]

It appears you need to add a shebang to your first line.

Add this before your current code and report back:
Code: Select all
#!/usr/bin/env python

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Syntax errors in code I copied from help site

Postby stranac » Wed Apr 16, 2014 7:21 am

As Mek said, it looks like your script is being executed by the shell.
Another option would be running it with python explicitly
Code: Select all
python myscript.py


You can switch two values in python without using a temp variable, just do:
Code: Select all
a, b = b, a

Also, bubble sort isn't really useful in python, but I'm guessing you're in some class that makes you do it, or just learning some general algorithms.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Syntax errors in code I copied from help site

Postby jdubya » Wed Apr 16, 2014 6:35 pm

Mekire wrote:I find no syntax errors in the above program.
It prints this, as expected:
Code: Select all
[17, 20, 26, 31, 44, 54, 55, 77, 93]

It appears you need to add a shebang to your first line.

Add this before your current code and report back:
Code: Select all
#!/usr/bin/env python

-Mek

That worked. I thought that line was just a comment and was ignored. I never used the shebang in the bash code I wrote. Thanks for the help!
jdubya
 
Posts: 4
Joined: Wed Apr 16, 2014 5:10 am

Re: Syntax errors in code I copied from help site

Postby micseydel » Wed Apr 16, 2014 8:49 pm

jdubya wrote:That worked. I thought that line was just a comment and was ignored. I never used the shebang in the bash code I wrote. Thanks for the help!

It is ignored by Python, but when you invoke your program like so
Code: Select all
$ ./PythonBubbleEx.py

you're telling your shell that you want to run the Python script, but your shell doesn't know Python. You didn't need the shebang for your bash scripts because you're probably using a bash shell, which already understands bash. But you have to tell it where Python is to use Python.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1260
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], ivan614969, Majestic-12 [Bot], snippsat, W3C [Linkcheck] and 3 guests