Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping While Loops

Counter question

why does the counter += 1 go at the end of the entire in if block

4 Answers

it definitely feels like that at times, but i guess that is part of the process. Thanks Ryan! really appreciate the help

Ryan Groom
Ryan Groom
18,614 Points

Andrew Uzoka The counter += 1 is actually in the "while" loop and not inside of the "if" conditional. (Indentation is an important key to understand in python programming). The reason it is there is because the code is counting every time a user attempts to guess the password. So while the password != 'opensesame' in other words if the password the user typed doesn't match then it updates the counter by 1 for every time a user makes an attempt to guess it. This way once they make 3 attempts, any additional attempt will exit the program and give them a message stating "Too many invalid password attempts". I hope this helps, if you need any clarification let me know.

Dantee Fluellen
Dantee Fluellen
1,499 Points

yeah I ask myself why did it give him four tries. Thanks a lot for this.

yeah that makes sense assume i wanted to make the password to only have int values, would i use a if statement to check against if it is a string the raise a value error e.g if passoword != str: raise valueError("This is not an int")

just trying to attempt a few things

Ryan Groom
Ryan Groom
18,614 Points

Andrew Uzoka Yes, you could use an if statement to test if the input value was of a certain type. I am not extremely familiar with Python, I am a web developer so I mostly use HTML, CSS, and Javascript, I just know very basics of Python as well as general programming, but without writing the code in an editor, I could not tell you the exact code to test for a string, but I'm sure you could very easily find it on Stack Overflow.

okay thanks. is it natural to have these difficulties when your starting out? as you immerse yourself do you start to understand and does it get easier over time ?

Ryan Groom
Ryan Groom
18,614 Points

Andrew Uzoka It's extremely natural. Everyone in this field feels completely lost in the beginning. I felt the same way, and eventually you start to understand the way things work and find new tools to help you figure out what is causing your errors, and better understand error messages. There are still times when I feel completely lost if there's a bug in my code, but I am a lot more comfortable than I used to be. Trust me, you are not alone. Everyone I've ever spoken to who studies computer science goes through it. Don't let it discourage you. Best of luck!