Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Gurutej Battaram
Gurutej Battaram
2,224 Points

input function returning NameError

I just installed Python 3.5.1 The input function keeps returning NameError. Can't figure out what is wrong. Please advice.

Gurutej Battaram
Gurutej Battaram
2,224 Points

The code :

def sillycase(string):
  s_lwr = string[0:int(len(string)/2)+1]
  s_upr = string[int(len(string)/2)+1:len(string)]
  return s_lwr.lower() + s_upr.upper()

string = input("Enter string: ")
string1 = sillycase(string)
print(string1)

The exception thrown :

Enter string: dasas
Traceback (most recent call last):
  File "hlf_upr_hlf_lwr.py", line 9, in <module>
    string = input("Enter string: ")
  File "<string>", line 1, in <module>
NameError: name 'dasas' is not defined

5 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Sounds like you're running it with Python 2 instead of 3. Try python --version and let me know what you get.

Gurutej Battaram
Gurutej Battaram
2,224 Points

I think it is Python 3. It does say 3.5.1

Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information.

WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. Visit http://www.python.org/download/mac/tcltk/ for current information.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That's definitely weird. What happens if you run your script with python3 instead of python?

Gurutej Battaram
Gurutej Battaram
2,224 Points

That's the thing. I can't find python3 in the drop down list. I opened the preferences window in Python Launcher. In that window, I clicked on the drop down list for selecting the interpreter. It doesn't list Python3. It only lists python and pythonw. pythonw is set as the default. I tried with python too, but I keep getting back the same error. Also, I checked for the version of python in Terminal (I use a macbook air). It says its running Python 2.7.10 I can't figure how to change it to Python 3.5 Help please?

Gurutej Battaram
Gurutej Battaram
2,224 Points

Figured it out. Thanks a bunch guys !! I'll have to run the scripts with IDLE which is python 3.5. Running them through Terminal is causing the problem, since the terminal is running python 2.7.