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

Carlos Pinzon
Carlos Pinzon
570 Points

while loop versus no while loop.

Kenneth solved this using a While True Loop. I thought this was a very smart way to overcome the exception, so that when you enter something different than the year born ,and have to enter your name over in order to get to the "What year were you born?" question. Since in the exception, there is a "continue", I thought that as soon you entered a wrong input for year, it would ask again "what year were you born?", but it doesnt, the exception is not working so I dont understand whats useful about this solution to overcome when something different that an integer is entered.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Remember to include a reference to which video you are referencing. Can you post the code that's not working?

Carlos Pinzon
Carlos Pinzon
570 Points

Hello Chris! I am sorry, I thought questions would pertain to the video specific or the worshop being done at the moment, my bad. The loop used my Kenneth in Python 3 is very simple

>>>while True:
>>>    birthday_year = input("What year were you born? ")
>>>    try:
>>>        birth_year = int(birth_year)
>>>    except ValueError:
>>>       continue
>>>    else:
>>>       break

I would think that when we "input" something that is not a whole number, so instead of putting 1988 I used my lastname (Pinzon), then there is a ValueError. That would fall within the exception and trigger the loop control "continue", which would make the cycle repeat until I actually "input" a whole number. But it doesnt do that, it just says that there is an error and stops the script. Thank you in advance! PS- I dont know why the code wont stay indented nor in separate lines when I save this comment

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

If the help button on the video page or challenge page is used to post the question, it should automatically include a link in the forum post. It does not add the link if posted directly in the forum. No worries. About your code....

When running the while loop, I get the error:

What year were you born? y
Traceback (most recent call last):
  File "<string>", line 4, in <module>
NameError: name 'birth_year' is not defined

The error raised is a NameError not a ValueError. This comes from a coding error in line 4:

birth_year = int(birth_year) # typo!

# should be

birth_year = int(birthday_year)

Post back if you need more help. Good luck!!!

Carlos Pinzon
Carlos Pinzon
570 Points

thank you so much !
yes there was a typo, I took both ValueError and NameError from the Exception, and joined with my fixed variable typo the program runs perfect. Thank you very much.