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 Object-Oriented Python (retired) Inheritance Instance Methods

Welby Obeng
Welby Obeng
20,340 Points

Why do you test if input is in sab? why not just start comparing input with s,a, or b?

Why do you test if input is in sab? why not just start comparing input with s,a, or b?

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Better to drop out quickly if they didn't give valid input. Fail early and often.

Welby Obeng
Welby Obeng
20,340 Points

so if i say give me a number 1 to 5, its better to check if input in "12345"? than to say

if input == 1

if input == 2

etc?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Probably. I can see arguments for doing it/not doing it, but I'd err on the side of caution.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Means to pick a certain direction even if it might not work 100% of the time, generally because you feel it's the safer choice.

For future reference, in this specific lesson, there's no check to see if a user inputs something like 'sa' or 'sab' or 'bas' (Try any combination of s, a and b that aren't just single letters). Those obviously don't make sense in this context, and you'd probably want additional checks later on if this were your own project.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You'd probably end up with more options than just three in a more serious game, so, yeah, you'd want better checks. Or even just changing it to if input in list('sab'): would stop it from matching "sa" or "ab".