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 trialGurutej Battaram
2,224 Pointsinput 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
2,224 PointsThe 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
Treehouse Guest TeacherSounds like you're running it with Python 2 instead of 3. Try python --version
and let me know what you get.
Gurutej Battaram
2,224 PointsI 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
Treehouse Guest TeacherThat's definitely weird. What happens if you run your script with python3
instead of python
?
Gurutej Battaram
2,224 PointsThat'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?
Haider Ali
Python Development Techdegree Graduate 24,728 PointsVisit this link and you can download python 3.5.
Gurutej Battaram
2,224 PointsFigured 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.
Haider Ali
Python Development Techdegree Graduate 24,728 PointsHaider Ali
Python Development Techdegree Graduate 24,728 PointsCan you please paste your code?