You could place the question loops in a general purpose function that returns a boolean. This would allow the rest of the code to use and/or logic directly:
def getAnswer(question):
while True:
answer = input(question)
if answer in ["yes","no"]: return answer == "yes"
def func1():
while True:
if getAnswer("Is it raining?: ") \
and not getAnswer("Have an umbrella?: "):
while getAnswer("Wait a while.\nIs it still raining?: "): pass
print("go outside...")
Another approach is to build "meta-data" (i.e. data that will drive the behaviour of a generic program). This allows you to create a completely different decision tree without writing a new function/program.
def decide(decisions, step="Q1"):
while True:
prompt,*nextSteps = decisions[step]
if "Q" in step: answer = input(prompt+": ")
else: answer = print(prompt) or "no"
if answer not in ["yes","no"]: continue
step = nextSteps[answer=="yes"]
In this case, the meta-data is a dictionary of steps that can be yes/no questions or simple printed statements. Each step indicates what is the next step either as a choice between two (for questions) or a single next step (for statements).
example1:
rainyDay = { "Q1":("Is it raining?","P1","Q2"),
"P1":("go outside...","Q1"),
"Q2":("Have an umbrella?","P2","P1"),
"P2":("Wait a While.","Q3"),
"Q3":("Is it still raining?","P1","P2")
}
decide(rainyDay)
Is it raining?: no
go outside...
Is it raining?: r
Is it raining?: yes
Have an umbrella?: yes
go outside...
Is it raining?: yes
Have an umbrella?: no
Wait a While.
Is it still raining?: no
go outside...
Is it raining?: yes
Have an umbrella?: yes
go outside...
Is it raining?:
example2:
happiness = { "Q1":("Do you have a problem ?","P1","Q2"),
"P1":("Be Happy!","Q1"),
"Q2":("Can you do something about it","P1","P2"),
"P2":("Do It!","Q1"),
}
decide(happiness)
Do you have a problem ?: no
Be Happy!
Do you have a problem ?: yes
Can you do something about it: no
Be Happy!
Do you have a problem ?: yes
Can you do something about it: yes
Do It!
Do you have a problem ?: no
Be Happy!
Do you have a problem ?: