I want to write something like a AI in Python:
My first code is like this:
def run() if choice_a_is_avilable: choice_a() return elif choice_b_is_avilable: if choice_b_1_is_avilable: choice_b_1() return elif choice_b_2_is_avilable: choice_b_1() return else: choice_c() while 1: run()
But the code to decide if choice_a_is_avilable is quite long, and condition should bind to method. I change the code to make it more clear.
def run(): if(choice_a()): return elif(choice_b()): return else(choice_c()): return def choice_b(): if choice_b_is_avilable: if(choice_b_1()): return True elif(choice_b_2) return True return False
When more and more choice comes into my code, it become more and more confusing and ugly, I considered using
Exception
:class GetChoiceException(Exception): """docstring for GetChoiceException""" def __init__(self, *args): super(GetChoiceException, self).__init__(*args) def run(): try: choice_a() choice_b() choice_c() except GetChoiceException: pass def choice_a(): if choice_a_is_avilable: some_function() raise GetChoiceException()
Is it a kind of abuse of Exception
?
What is the write way to make choices in Python?