import streamlit as st # Title of the game st.title("SUBR Game: Stack Underflow Bug Riddle") # Introduction st.write(""" Welcome to the SUBR Game! Your goal is to find the stack underflow bug in the given code snippet. Solve the riddle and submit your answer to see if you're correct! """) # Code snippet with a hidden bug code_snippet = """ def calculate_sum(numbers): total = 0 for i in range(len(numbers) - 1, -1, -1): total += numbers[i] return total numbers = [1, 2, 3, 4, 5] result = calculate_sum(numbers) print("The sum is:", result) """ st.code(code_snippet, language='python') # User input for the answer user_answer = st.text_input("What is the stack underflow bug in the code above?", "") # Correct answer (hidden) correct_answer = "The bug is in the loop range; it should be `range(len(numbers) - 1, -1, -1)` to avoid underflow." # Check the user's answer if st.button("Submit"): if user_answer.strip().lower() == correct_answer.lower(): st.success("Congratulations! You found the bug!") else: st.error("Sorry, that's not correct. Try again!") # Footer st.write(""" This is a simple example of a SUBR game. You can expand it with more complex code snippets and riddles. """)