File size: 2,595 Bytes
575af79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d8399d
 
 
 
575af79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d8399d
 
 
 
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
70
71
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):
    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 bar
        progress = i / n
        progress_color = update_progress_bar_color(progress)

        # Progress bar
        st.progress(progress)
        st.markdown(f'<style>.stProgress {{"background-color": "{progress_color}";}}</style>', unsafe_allow_html=True)

        time.sleep(0.5)  # Simulate execution time
    
    # Display final results
    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)

# Function to demonstrate a while loop with colorful output
def demonstrate_while_loop(n):
    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 bar
        progress = i / n
        progress_color = update_progress_bar_color(progress)

        # Progress bar
        st.progress(progress)
        st.markdown(f'<style>.stProgress {{"background-color": "{progress_color}";}}</style>', unsafe_allow_html=True)

        time.sleep(0.5)  # Simulate execution time
        i += 1
    
    # Display final results
    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)

# 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)