[Tutor] python problems on android

Alan Gauld alan.gauld at btinternet.com
Sat Jan 4 14:30:38 CET 2014


On 04/01/14 11:24, blechnum at fireflyuk.net wrote:

> At the command line (on my computer)  I can type
>
> h = input(" enter character ")
>
> and when i type a response and enter it, then type h and return, i am
> given back the response i put in.
>
> I type j , get 'j' back
>
> Now on my phone i can do the same.
>
> And here is the odd bit
> If I type a number in, the response will be the number
>
> type 4, get '4' back
>
> but if i  type in a letter I get an error, the same error as if i had
> typed in the response to the command line.
>
> NameError: name '<response to input request>" is not defined


What you describe is the symptoms of using different versions of Python.

In Python v3 (your computer?) input reads a string from the user.
In Python v2 input reads a string and evaluates it as code.
Thus a number evaluates to a number but a string is evaluated like a 
variable name.

Presumably your Android is running your code under a v2 Python interpreter.

You can try putting raw_input() instead of input() in your code to see 
if that fixes it. [input() in v3 is a renaming of input() and the 
original input() function was removed.]

If that works for the simple test case you could try adding this at
the top of your script:

try: input = raw_input
except NameError: pass

But that only addresses one aspect of Python v2/3 incompatibility so 
there will probably be other fixes needed. The best thing is to align 
Python versions.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list