File size: 5,917 Bytes
d20cfd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "marimo",
# ]
# ///

import marimo

__generated_with = "0.10.14"
app = marimo.App()


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
        # πŸ”„ Conditional Logic in Python

        Learn how to make decisions in your code with Python's conditional statements!

        ## If Statements
        The foundation of decision-making in Python:
        ```python
        if condition:
            # code to run if condition is True
        elif another_condition:
            # code to run if another_condition is True
        else:
            # code to run if no conditions are True
        ```
        Let's explore with some examples:
        """
    )
    return


@app.cell
def _():
    number = 42
    return (number,)


@app.cell
def _(number):
    if number > 50:
        result = "Greater than 50"
    elif number == 42:
        result = "Found the answer!"
    else:
        result = "Less than 50"
    result
    return (result,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## Interactive Decision Making
        Try changing the conditions below and see how the results change (powered by marimo's UI elements!):
        """
    )
    return


@app.cell
def _(mo, threshold, value):
    mo.hstack([value, threshold])
    return


@app.cell
def _(mo):
    value = mo.ui.number(value=25, start=0, stop=100, label="Enter a number")
    threshold = mo.ui.slider(value=50, start=0, stop=100, label="Set threshold")
    return threshold, value


@app.cell
def _(mo, threshold, value):
    if value.value > threshold.value:
        decision = f"{value.value} is greater than {threshold.value}"
    elif value.value == threshold.value:
        decision = f"{value.value} is equal to {threshold.value}"
    else:
        decision = f"{value.value} is less than {threshold.value}"

    mo.hstack(
        [
            mo.md(f"**Decision**: {decision}"),
            mo.md(f"**Condition Status**: {'βœ…' if value.value >= threshold.value else '❌'}")
        ],
        justify="space-between"
    )
    return (decision,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## Boolean Operations
        Python uses boolean operators to combine conditions:

        - `and`: Both conditions must be True

        - `or`: At least one condition must be True

        - `not`: Inverts the condition
        """
    )
    return


@app.cell
def _(age, has_id, mo):
    mo.hstack([age, has_id])
    return


@app.cell
def _(mo):
    age = mo.ui.number(value=18, start=0, stop=120, label="Age")
    has_id = mo.ui.switch(value=True, label="Has ID")
    return age, has_id


@app.cell
def _(age, has_id, mo):
    can_vote = age.value >= 18 and has_id.value

    explanation = f"""
    ### Voting Eligibility Check

    Current Status:

    - Age: {age.value} years old

    - Has ID: {'Yes' if has_id.value else 'No'}

    - Can Vote: {'Yes βœ…' if can_vote else 'No ❌'}

    Reason: {'Both age and ID requirements met' if can_vote 
            else 'Missing ' + ('required age' if age.value < 18 else 'valid ID')}
    """

    mo.md(explanation)
    return can_vote, explanation


@app.cell(hide_code=True)
def _(mo):
    _text = mo.md("""
        - Try different combinations of age and ID status
        - Notice how both conditions must be True to allow voting
        - Experiment with edge cases (exactly 18, no ID, etc.)
    """)
    mo.accordion({"πŸ’‘ Experiment Tips": _text})
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
        ## Complex Conditions
        Combine multiple conditions for more sophisticated logic:
        ```python
        # Multiple conditions
        if (age >= 18 and has_id) or has_special_permission:
            print("Access granted")

        # Nested conditions
        if age >= 18:
            if has_id:
                print("Full access")
            else:
                print("Limited access")
        ```
        """
    )
    return


@app.cell
def _(humidity, mo, temp, wind):
    mo.hstack([temp, humidity, wind])
    return


@app.cell
def _(mo):
    temp = mo.ui.number(value=25, start=-20, stop=50, label="Temperature (Β°C)")
    humidity = mo.ui.slider(value=60, start=0, stop=100, label="Humidity (%)")
    wind = mo.ui.number(value=10, start=0, stop=100, label="Wind Speed (km/h)")
    return humidity, temp, wind


@app.cell
def _(humidity, mo, temp, wind):
    def get_weather_advice():
        conditions = []
        
        if temp.value > 30:
            conditions.append("🌑️ High temperature")
        elif temp.value < 10:
            conditions.append("❄️ Cold temperature")
                
        if humidity.value > 80:
            conditions.append("πŸ’§ High humidity")
        elif humidity.value < 30:
            conditions.append("🏜️ Low humidity")
                
        if wind.value > 30:
            conditions.append("πŸ’¨ Strong winds")
                
        return conditions
        
    conditions = get_weather_advice()
        
    message = f"""
    ### Weather Analysis

    Current Conditions:

    - Temperature: {temp.value}Β°C

    - Humidity: {humidity.value}%

    - Wind Speed: {wind.value} km/h

    Alerts: {', '.join(conditions) if conditions else 'No special alerts'}
    """

    mo.md(message)
    return conditions, get_weather_advice, message


@app.cell(hide_code=True)
def _(mo):
    callout_text = mo.md("""
    ## Your Logic Journey Continues!

    Next Steps:

    - Practice combining multiple conditions

    - Explore nested if statements

    - Try creating your own complex decision trees (pun on an ML algorithm!)

    Keep coding! 🎯✨
    """)

    mo.callout(callout_text, kind="success")
    return (callout_text,)


if __name__ == "__main__":
    app.run()