# Python-only Strings Quiz
from IPython.display import display, HTML, Javascript
import ipywidgets as widgets
print("🐍 Python Strings — Free-Response Quiz")
print("Answer the questions below and click Check Answers.\n")
# Q1: Slice
q1_label = widgets.HTML("<b>1. What does 'hello'[1:4] return?</b>")
q1 = widgets.Text(placeholder='e.g., ell')
# Q2: Length
q2_label = widgets.HTML("<b>2. What is len('Escape Room')?</b>")
q2 = widgets.Text(placeholder='Enter a number')
# Q3: Lowercase
q3_label = widgets.HTML("<b>3. What does 'Python'.lower() return?</b>")
q3 = widgets.Text(placeholder='e.g., python')
# Q4: Strip
q4_label = widgets.HTML("<b>4. If s = ' CS Rocks! ', what does s.strip() return?</b>")
q4 = widgets.Text(placeholder='e.g., CS Rocks!')
# Q5: startswith (True/False)
q5_label = widgets.HTML("<b>5. Does 'algorithm' start with 'algo'?</b> (True/False)")
q5 = widgets.Text(placeholder='True/False')
# Q6: count
q6_label = widgets.HTML("<b>6. What is 'banana'.count('an')?</b>")
q6 = widgets.Text(placeholder='Enter a number')
# Q7: find
q7_label = widgets.HTML("<b>7. What index does 'debuggers'.find('bug') return?</b>")
q7 = widgets.Text(placeholder='Enter a number')
submit_btn = widgets.Button(description='Check Answers', button_style='success', icon='check')
output = widgets.Output()
def parse_bool_like(s: str):
v = s.strip().lower()
if v in {'true','t','yes','y','1'}: return True
if v in {'false','f','no','n','0'}: return False
return None
def strip_quotes(s: str):
return s.strip().strip("'").strip('"')
def clean_str(s: str):
return strip_quotes(s).strip()
def check_answers(btn):
with output:
output.clear_output()
score, total = 0, 7
print('📊 QUIZ RESULTS')
print('='*40)
# Q1
a1 = clean_str(q1.value).lower()
if a1 == 'ell':
score += 1; print('✅ Q1: CORRECT')
else:
print('❌ Q1: INCORRECT — Correct: ell')
# Q2 (len of 'Escape Room' is 11)
a2 = clean_str(q2.value)
if a2.isdigit() and int(a2) == 11:
score += 1; print('✅ Q2: CORRECT')
else:
print('❌ Q2: INCORRECT — Correct: 11 (spaces count)')
# Q3
a3 = clean_str(q3.value).lower()
if a3 == 'python':
score += 1; print('✅ Q3: CORRECT')
else:
print("❌ Q3: INCORRECT — Correct: python")
# Q4
a4 = strip_quotes(q4.value)
if a4 == 'CS Rocks!':
score += 1; print('✅ Q4: CORRECT')
else:
print("❌ Q4: INCORRECT — Correct: CS Rocks!")
# Q5
a5 = parse_bool_like(q5.value)
if a5 is None:
print('⚠️ Q5: Could not parse. Please answer True/False.')
elif a5 is True:
score += 1; print('✅ Q5: CORRECT')
else:
print("❌ Q5: INCORRECT — Correct: True")
# Q6
a6 = clean_str(q6.value)
if a6.isdigit() and int(a6) == 2:
score += 1; print('✅ Q6: CORRECT')
else:
print("❌ Q6: INCORRECT — Correct: 2")
# Q7 ('debuggers'.find('bug') is 2)
a7 = clean_str(q7.value)
if a7.isdigit() and int(a7) == 2:
score += 1; print('✅ Q7: CORRECT')
else:
print("❌ Q7: INCORRECT — Correct: 2")
print('='*40)
print(f'🎯 Your Score: {score}/{total}')
print(f'📈 Percentage: {score/total*100:.1f}%')
try:
display(Javascript("localStorage.setItem('csp34_hw_py_quiz_tried','true');"))
except Exception:
pass
submit_btn.on_click(check_answers)
display(q1_label, q1, q2_label, q2, q3_label, q3, q4_label, q4, q5_label, q5, q6_label, q6, q7_label, q7, submit_btn, output)