Python 3.4 - error with input function

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

Python 3.4 - error with input function

Postby AlexK » Fri Apr 04, 2014 4:40 pm

Hi-
I'm new to Python and have installed v3.4 on my Mac (v10.8.5). I'm running the simple program:
Code: Select all
person = input('Enter your name: ')
print ('Hello ', person)

When I run the script I get the following:
Code: Select all
Enter your name: alex

Traceback (most recent call last):
  File "/Applications/Python 3.4/hello_you.py", line 1, in <module>
    person = input('Enter your name: ')
  File "<string>", line 1, in <module>
NameError: name 'alex' is not defined

Looks like the string 'alex' is not being assigned correctly to the variable 'person'. My understanding is that the 'input' function should do this translation. If I enter a integer value the script works fine. What am I missing?

Thanks,
Alex
Last edited by Mekire on Fri Apr 04, 2014 4:42 pm, edited 1 time in total.
Reason: First post lock.
AlexK
 
Posts: 2
Joined: Fri Apr 04, 2014 4:25 pm

Re: Python 3.4 - error with input function

Postby Mekire » Fri Apr 04, 2014 4:48 pm

That code runs perfectly fine in python 3.

It appears that you are, despite the folder you are in, invoking a python 2 interpreter.

Results with both interpreters:
Code: Select all
C:\Users\Owner\Desktop>python my_file.py
Enter your name: alex
Traceback (most recent call last):
  File "my_file.py", line 1, in <module>
    person = input('Enter your name: ')
  File "<string>", line 1, in <module>
NameError: name 'alex' is not defined

C:\Users\Owner\Desktop>python3 my_file.py
Enter your name: alex
Hello  alex

C:\Users\Owner\Desktop>

Please run this and post the result:
Code: Select all
import sys
print(sys.version)

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

Re: Python 3.4 - error with input function

Postby AlexK » Fri Apr 04, 2014 5:30 pm

You are correct, here is the result:

2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]

So, the next newbie question is how do I get my CI updated. I have downloaded 3.4 and I'm starting Idle from that folder, but I obviously do not have something linked properly.

Thanks,
Alex
AlexK
 
Posts: 2
Joined: Fri Apr 04, 2014 4:25 pm

Re: Python 3.4 - error with input function

Postby snippsat » Fri Apr 04, 2014 6:07 pm

Both Linux and Mac comes with Python installed.
I don't use Mac,but seems like Macports is the right way for multiple Python versions on Mac.
http://calvinx.com/2010/02/02/python-on-mac/
http://www.insomnihack.com/?p=442

Python 2.7 is still the versions most people use,Python 3.4--> is of course the future.
Many 3 part library are still only for Python 2,but the status is getting better.
User avatar
snippsat
 
Posts: 163
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests