0

I was tasked with the following practice problem:

(Bubble sort) Write a sort function that uses the bubble-sort algorithm. The bubble-sort algorithm makes several passes through the list. On each pass, successive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their way to the top and the larger values “sink” to the bottom. Write a test program that reads in ten numbers, invokes the function, and displays the sorted numbers.

This is what I got so far,

def bubbleSort(array):
    for i in range(len(array)):
        for j in range(len(array) - 1, i, -1):
            if (array[j] < array[j - 1]):
                tmp = array[j]
                array[j] = array[j - 1]
                array[j - 1] = tmp

def printElm(array):
    print("\n\n");
    for i in range(len(array)):
        print(" " + str(array[i]), end= " ")

def main():
    list = [];
    for i in range(10):
        value = int(input('\n Enter Value ' + str(i + 1) + ": "));
        list.append(value);
printElm(list);
bubbleSort(list);
printElm(list);
print("\n\n");

main();

But I keep getting the following error:

 print(" " + str(array[i]), end= " ")
                              ^
 SyntaxError: invalid syntax

What is frustrating is that the the textbook references Python 3 while the professor wants work done in with Pycharm which is Python 2.6.

  • How can I possibly solve this error?
A.luu
  • 11
  • 3
  • For this particular problem you can place `from __future__ import print_function` at the beginning of the file. – Michael Butscher Nov 17 '17 at 03:09
  • @MichaelButscher Thank you for your help! – A.luu Nov 17 '17 at 03:14
  • Did you get the `end=" "` code from the textbook? Is it possible that the professor addressed that by saying "oh, you can't use that, just leave it out" during the lecture, but you weren't paying attention? – John Gordon Nov 17 '17 at 03:14
  • [This question](https://stackoverflow.com/a/2456292) seems to address your problem pretty well. – ricky3350 Nov 17 '17 at 03:16
  • I believe that in Python2, you can replace your print statement with `print " " + str(array[i]),` The ending comma in python2 is just like the `end=" "` in python3. – John Anderson Nov 17 '17 at 03:17
  • Just to make that clear: PyCharm is not bound to a special Python version. – Klaus D. Nov 17 '17 at 03:20

0 Answers0