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 trialThiago Paulo
3,076 PointsProgram not calling the input
Hi, I have being trying to use the same loggic for a validation of a yes or no question but, the input is just not running, I'm not sure if i need something else to make the code run automatically.
def choice(self):
choice = input("[Y] or [N]o").upper()
if choice in 'YN':
if choice == 'Y':
print("\n\n\n\t\t\t\tIt Works!!!!!")
else:
print("\n\n\n\t\t\t\tIt still works!!!!!")
else:
return self.choice()
[MOD: added ```python formatting -cf]
2 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsWhat class is this method part of? How are you instantiating it and calling this method?
Are you doing something like this?
this_inst = SomeModel()
this_inst.choice()
If you wanted it to be a standalone function, save the following to a file called choise.py
:
def choice():
choice = input("[Y] or [N]o").upper()
if choice in 'YN':
if choice == 'Y':
print("\n\n\n\t\t\t\tIt Works!!!!!")
else:
print("\n\n\n\t\t\t\tIt still works!!!!!")
else:
return choice()
# python idiom to execute if file called directly from command line
if __name__ == '__main__':
# call function
choice()
Then, at a shell prompt, type python choice.py
Thiago Paulo
3,076 PointsThat might be my problem,that is all I wrote. I did it as a test, because I found it interesting the idea of making a validation test with a simple replacement for the 'C' "switch" function. I'm not sure what more do I need.
Chris Freeman
Treehouse Moderator 68,454 Pointsadded code to answer for running the file directly.