Trouble getting started with Pythonista
I'm a retired programmer and I'm new to python. My question is so simple it's embarrassing. I installed python on my desktop and I also installed Pythonista on my iPad. The following simple program works perfectly on the desk top, but not on my iPad:
input_var = ("enter your name: ")
print("Hello! " + input_var)
on the PC this works, but in Pythonista on the iPad, when I enter my name, Paul, the print statement fails with "NameError: name 'Paul' is not defined." I must be doing something stupid, but I don't see what. Any help?
I'm a Python novice as well, so I'll defer to those more experienced on the forum.
It seems that your whole code is running at once, with
input_varbeing set equal to the string on line 1, then everything being printed at line 2. When you enter "Paul" it is like entering "Paul" as a statement, which is not recognised.
The code below will work.
input_varequal to the users input.
input_var = raw_input("enter your name: ") print("Hello! " + input_var)
thanks, I'll give it a try.
As for why it's behaving differently on the PC, I'm not sure! :)
I am not exactly sure of the syntax in Python 3.0+ but the current version of Python that Pythonista uses is 2.7. Perhaps this is the reason?
Also, I believe in Python 3.0 the input() function is used for strings and integers and the raw_input() function has been deprecated in 3.0.
@TutorialDoctor That's a good point. As a neophyte I really wasn't conscious of he differences between releases. I'm running 3.51 on the PC
Thanks to everyone. that raw_input function did the trick. I think the Beginning Python book I had out of the library was geared to release 3. I'm sure I'll back with more questions. This is a far cry from assembler language programming on the old IBM mainframes!
Python 2 and 3 incompatibility is something you'll have to get used to. Python 2 is the backwards-compatible legacy version, and Python 3 is the actively developed but backwards-incompatible version. Which means that you have strange differences like with
Under Python 3, there is just
input, which asks the user for a line of text input and returns it. Under Python 2 that is done with
raw_input. The Python 2
inputis different - it takes user input like
raw_input, but then reads it as a Python expression and returns it. This is cumbersome to use and a security risk, which is why in Python 3,
raw_inputwas renamed to
@dgelessus In your opinion, should I be concentrating on 3.0? Is that going to be the standard going forward?
@paulgottlieb Python 3 is generally the better choice, if you have the choice. Sometimes you have to use Python 2 because you're using some other component that only supports Python 2, such as Pythonista in this case.
But if you do have the choice, I strongly recommend Python 3, as it has far less special cases that exist only for backwards compatibility, like
unicodeconversion, old-style and new-style classes, etc. If you're going to work with Python 2, you'll need to learn about these things at some point, but if you're just starting with Python I'd recommend Python 3 - it's a lot cleaner and easier to understand in a few cases.
The current version of Pythonista only supports Python 2, but a version with both Python 2 and 3 is currently in beta (if you're interested in testing it, see the forum thread on the topic). As far as I know it will be released as a separate app once it's finished.