import streamlit as st import time # Function to update the progress bar color based on the progress def update_progress_bar_color(progress): if progress < 0.3: return '#800080' # Purple elif progress < 0.7: return '#00ffff' # Cyan else: return '#00ff00' # Lime Green # Function to demonstrate a for loop with colorful output def demonstrate_for_loop(n): progress_bar = st.progress(0) # Initialize the progress bar st.markdown('

For Loop Executing...

', unsafe_allow_html=True) result = "" for i in range(1, n + 1): color = "teal" if i % 2 == 0 else "orange" result += f'{i} ' # Update the progress progress = i / n progress_bar.progress(progress) # Display results in real-time st.markdown(f'
' f'For Loop result: {result}
', unsafe_allow_html=True) time.sleep(0.5) # Simulate execution time # Function to demonstrate a while loop with colorful output def demonstrate_while_loop(n): progress_bar = st.progress(0) # Initialize the progress bar st.markdown('

While Loop Executing...

', unsafe_allow_html=True) result = "" i = 1 while i <= n: color = "teal" if i % 2 == 0 else "orange" result += f'{i} ' # Update the progress progress = i / n progress_bar.progress(progress) # Display results in real-time st.markdown(f'
' f'While Loop result: {result}
', unsafe_allow_html=True) time.sleep(0.5) # Simulate execution time i += 1 # Streamlit app layout st.title("Loop Demonstrator App") st.markdown("This app demonstrates a for loop and a while loop with colorful outputs.") # User inputs n = st.number_input("Enter a number:", min_value=1, value=1) loop_type = st.selectbox("Select Loop Type:", ["For Loop", "While Loop"]) # Button to trigger the loop demonstration if st.button('Run Loop'): if loop_type == 'For Loop': demonstrate_for_loop(n) else: demonstrate_while_loop(n)