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 Python Basics (2015) Logic in Python The Exception to the Rule

Zeljko Porobija
Zeljko Porobija
11,491 Points

except yields with different result

I used the code from teacher's notes and, say, I write some instead of 5 - I get this Traceback (most recent call last): File "exceptions.py", line 2, in <module> speed = int(input("What is the airspeed velocity of an unladen swallow? ")) File "<string>", line 1, in <module> NameError: name 'Some' is not defined

3 Answers

Nathan Tallack
Nathan Tallack
22,159 Points

I am not sure why you are getting that error. I try the teachers example code and it works just fine for me.

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 "help", "copyright", "credits" or "license" for more information.
>>> try:
...     speed = int(input("What is the airspeed velocity of an unladen swallow? "))
... except ValueError:
...     print("What? I don't kno-oooooooooooooooo!")
... else:
...     print("I think a swallow could go faster than {}.".format(speed))
... 
What is the airspeed velocity of an unladen swallow? 5
I think a swallow could go faster than 5.
>>> try:
...     speed = int(input("What is the airspeed velocity of an unladen swallow? "))
... except ValueError:
...     print("What? I don't kno-oooooooooooooooo!")
... else:
...     print("I think a swallow could go faster than {}.".format(speed))
... 
What is the airspeed velocity of an unladen swallow? Some
What? I don't kno-oooooooooooooooo!
>>> 

Can you show me your code if it looks different to this?

Hi Nathan, Why we can't change the words "ValueError" to other one, because ValueError is a standard here? I thought this is just a variable.

Zeljko Porobija
Zeljko Porobija
11,491 Points

Well, I think I found out the reason. I don't do coding in the Workspace, due to {} and [] issues (yep, it doesn't work neither on Chrome nor on Mozilla). I use just a simple Notepad++ editor instead (because that's how I learn coding, no IDE, but a simple editor). And I have Python 2.7 installed on my PC (some time ago, it was a deliberate decision). Ok, it's time to move on Python 3.5. Thanks for your time, it helped me to see that it works in principle.

If you are running script from Python on your pc ( I had the same problem) - I changed the except to NameError instead of ValueError. Not sure if it matters if I have to except lines - I just added both lines.

except ValueError: print(something) except NameError: print(something)

works fine now