-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathfollowup_questions_demo.py
More file actions
89 lines (71 loc) · 3.01 KB
/
followup_questions_demo.py
File metadata and controls
89 lines (71 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Demonstration of the add_followup_questions() method
This demonstrates the new syntactical sugar feature for adding follow-up questions
based on multiple choice or checkbox question options, as requested in GitHub issue #2231.
"""
from edsl import QuestionMultipleChoice, QuestionFreeText, Survey
# Create a multiple choice question
q_restaurants = QuestionMultipleChoice(
question_name="restaurants",
question_text="Which type of restaurant do you prefer?",
question_options=["Italian", "Chinese", "Mexican", "Indian"]
)
# Create a follow-up question template
# The {{ restaurants.answer }} will be replaced with each option
q_followup = QuestionFreeText(
question_name="why_restaurant",
question_text="Why do you like {{ restaurants.answer }} food?"
)
# Create a final question
q_overall = QuestionFreeText(
question_name="overall_feedback",
question_text="Any other comments about restaurants?"
)
# Build the survey with automatic follow-ups
survey = Survey([q_restaurants, q_overall]).add_followup_questions("restaurants", q_followup)
print("="*70)
print("Survey Structure with Follow-up Questions")
print("="*70)
print("\nQuestions in the survey:")
for i, q in enumerate(survey.questions):
print(f"{i}. {q.question_name}")
print(f" {q.question_text}")
print("\n" + "="*70)
print("Survey Flow Demonstration")
print("="*70)
# Demonstrate the flow for different answers
test_cases = ["Italian", "Chinese", "Mexican", "Indian"]
for answer in test_cases:
print(f"\n--- When user selects '{answer}' ---")
current = None
answers = {}
step = 0
# Simulate going through the survey
questions_shown = []
while step < 10: # Safety limit
from edsl.surveys.base import EndOfSurvey
next_q = survey.next_question(current, answers)
# Check if we've reached the end
if next_q == EndOfSurvey or (hasattr(next_q, '__class__') and
next_q.__class__.__name__ == 'EndOfSurveyParent'):
break
questions_shown.append(next_q.question_name)
# Simulate answering
if next_q.question_name == "restaurants":
answers["restaurants.answer"] = answer
elif next_q.question_name.startswith("why_restaurant"):
answers[f"{next_q.question_name}.answer"] = "Sample answer"
elif next_q.question_name == "overall_feedback":
answers["overall_feedback.answer"] = "Great!"
current = next_q.question_name
step += 1
print(f"Questions shown: {' -> '.join(questions_shown)}")
print("\n" + "="*70)
print("\nKey Features:")
print("- Automatically creates one follow-up question per option")
print("- Substitutes {{ restaurants.answer }} with the actual option text")
print("- Adds skip logic so each follow-up only shows for its option")
print("- Maintains proper survey flow to the next question after follow-ups")
print("\nThis eliminates the need for manually creating and wiring up")
print("conditional follow-up questions for each option!")
print("="*70)