Forum Archive

Running a program on Pythonista and Sublime Text.

clarence

Hi,

I am new to python, and I am learning to program on the mac using Sublime Text and Pythonista on the iPad. The code below executes on the mac with no issues, but on the iPad, I get an "invalid syntax" on line 19.

Line 19: print median (1,3,2) is the output of the program.

Is there a different way to getting the output of this program with Pythonista?

Thanks for your help in advance.

Clarence.

def bigger(a,b):
    if a > b:
        return a
    else:
        return b

def biggest(a,b,c):
    return bigger(a,bigger(b,c))

def median(a,b,c):
    big = biggest(a,b,c)
    if big == a:
        return bigger(b,c)
    if big == b:
        return bigger(a,c)
    else:
        return bigger (a,b)

print median (1,3,2)
zipit

You are most likely running the script on your mac on Python 2.7 and on the iPad on Python 3.5 (Pythonistas default). With Python 3.0 the Python 2.xprint keyword got demoted to a function and therefore does now require parentheses to be invoked. Long story short, you can either:

  • set Pythonista to Python 2.7 in the settings golbally
  • or just for one instance by long pressing the play button
  • or you could add something called a shebang line, forcing python into Python 2 mode from code, the first line of your script has to be #!Python2
  • or just change the code to print (median (1,3,2)), which in this case will also run just fine under Python 2.
clarence

Your suggestions worked.

Perfect - thanks!

ccc

My approach would be to put from __future__ import print_function at the top of your file before all other import statements. This will force you to write code that works on both versions of Python.

Also, bigger() could be replaced by max() https://docs.python.org/3/library/functions.html#max

omz

@ccc

Also, bigger() could be replaced by max()

I'm guessing this is mostly just an exercise... Otherwise, in Python 3.x, median could also be replaced by statistics.median.

zipit

My approach would be to put from future import print_function

I do not want to be rude, but using future is IMHO bad advice ( especially for a beginner). IMHO it also goes against the Google Python style guide lines, which while not speaking out explicitly agianst future do forbid the use of deprecated methods and unusal styles in multiple guide lines.

omz

@zipit I'm not sure if I'd recommend it to a beginner – it might be better to just use one of the two Python versions, and learn about the differences and porting/compatibility strategies later – but __future__ imports aren't deprecated or unusual at all. If you want a code base that supports both versions, this is definitely the right approach.

ccc

@zipit Last time I looked Google was not the governing authority over the Python language. ;-). In fact, looking at their Grumpy project, I would say that Google would rather see developers move from Python 2 to Golang rather than to Python 3. As @omz points out, __future__ is part of the standard library (introduced in Python 2.1) and is not deprecated. http://python-future.org

zipit

@omz

Jeah, I did not state that very clearly. Was I meant was that since they forbid using deprecated methods the (kinda) opposite - future imports - are also not a good idea as mixing versions goes against the zen-priniciple concise and clear.

edit: And what is common is IMHO not an indicator of what is good. People also use cryptic bit shifts, overly complex list comprehensions, etc. all over the place and they are also not good.

But that is just my opinion. But you are right there are cases where you need future.