File size: 2,557 Bytes
575af79 f2cc07a 0096d42 575af79 f2cc07a 575af79 f2cc07a 575af79 0096d42 575af79 f2cc07a 0096d42 575af79 f2cc07a 575af79 f2cc07a 575af79 0096d42 575af79 |
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 |
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('<h3 style="color:#1E90FF;">For Loop Executing...</h3>', unsafe_allow_html=True)
result = ""
for i in range(1, n + 1):
color = "teal" if i % 2 == 0 else "orange"
result += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>'
# Update the progress
progress = i / n
progress_bar.progress(progress)
# Display results in real-time
st.markdown(f'<div style="background-color:#fffacd; padding:20px; border-radius:10px; box-shadow: 0px 4px 12px rgba(0,0,0,0.1);">'
f'<strong>For Loop result:</strong> {result}</div>', 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('<h3 style="color:#1E90FF;">While Loop Executing...</h3>', unsafe_allow_html=True)
result = ""
i = 1
while i <= n:
color = "teal" if i % 2 == 0 else "orange"
result += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>'
# Update the progress
progress = i / n
progress_bar.progress(progress)
# Display results in real-time
st.markdown(f'<div style="background-color:#fffacd; padding:20px; border-radius:10px; box-shadow: 0px 4px 12px rgba(0,0,0,0.1);">'
f'<strong>While Loop result:</strong> {result}</div>', 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)
|