Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,8 +45,13 @@ lessons = [
|
|
| 45 |
- Topic C: Create a Python Application
|
| 46 |
- Topic D: Prevent Errors
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
""",
|
| 51 |
"starter_code": '''# Lesson 1: Simple Hello & Input
|
| 52 |
|
|
@@ -58,8 +63,11 @@ age = input("How old are you? ")
|
|
| 58 |
print(f"Hello, {name}! You are {age} years old.")
|
| 59 |
|
| 60 |
# Try:
|
| 61 |
-
# 1.
|
| 62 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
| 63 |
'''
|
| 64 |
},
|
| 65 |
{
|
|
@@ -71,8 +79,13 @@ print(f"Hello, {name}! You are {age} years old.")
|
|
| 71 |
- Topic A: Process Strings and Integers
|
| 72 |
- Topic B: Process Decimals, Floats, and Mixed Number Types
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
""",
|
| 77 |
"starter_code": '''# Lesson 2: Strings, Integers, Floats
|
| 78 |
|
|
@@ -93,8 +106,10 @@ print("BMI (unrounded):", bmi)
|
|
| 93 |
print("BMI (rounded to 2 decimals):", round(bmi, 2))
|
| 94 |
|
| 95 |
# Try:
|
| 96 |
-
# 1. Ask the user for their name and birth year
|
| 97 |
-
# 2. Convert a temperature from Fahrenheit to Celsius
|
|
|
|
|
|
|
| 98 |
'''
|
| 99 |
},
|
| 100 |
{
|
|
@@ -106,7 +121,13 @@ print("BMI (rounded to 2 decimals):", round(bmi, 2))
|
|
| 106 |
- Topic A: Process Ordered Data Structures (lists, tuples)
|
| 107 |
- Topic B: Process Unordered Data Structures (sets, dictionaries)
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
""",
|
| 111 |
"starter_code": '''# Lesson 3: Lists, Tuples, Sets, Dictionaries
|
| 112 |
|
|
@@ -136,13 +157,15 @@ student = {
|
|
| 136 |
|
| 137 |
print("Student name:", student["name"])
|
| 138 |
|
| 139 |
-
print("\
|
| 140 |
for key, value in student.items():
|
| 141 |
print(f"{key}: {value}")
|
| 142 |
|
| 143 |
# Try:
|
| 144 |
-
# 1. Add a new key "major" to the student dictionary.
|
| 145 |
# 2. Create a list of 5 movies and print the first and last.
|
|
|
|
|
|
|
| 146 |
'''
|
| 147 |
},
|
| 148 |
{
|
|
@@ -154,7 +177,13 @@ for key, value in student.items():
|
|
| 154 |
- Topic A: Write a Conditional Statement (`if`, `elif`, `else`)
|
| 155 |
- Topic B: Write a Loop (`for`, `while`)
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
""",
|
| 159 |
"starter_code": '''# Lesson 4: Conditionals & Loops
|
| 160 |
|
|
@@ -177,7 +206,7 @@ else:
|
|
| 177 |
print(f"Your grade is: {grade}")
|
| 178 |
|
| 179 |
# Loop example: print even numbers 2-20
|
| 180 |
-
print("\
|
| 181 |
for n in range(2, 21, 2):
|
| 182 |
print(n, end=" ")
|
| 183 |
|
|
@@ -186,6 +215,8 @@ print() # newline
|
|
| 186 |
# Try:
|
| 187 |
# 1. Use a while loop to ask for a password until it is 'secret'.
|
| 188 |
# 2. Write an if/elif/else to classify a number as positive, negative, or zero.
|
|
|
|
|
|
|
| 189 |
'''
|
| 190 |
},
|
| 191 |
{
|
|
@@ -198,7 +229,13 @@ print() # newline
|
|
| 198 |
- Topic B: Define and Instantiate a Class
|
| 199 |
- Topic C: Import and Use a Module
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
""",
|
| 203 |
"starter_code": '''# Lesson 5: Functions, Classes, and Modules
|
| 204 |
|
|
@@ -239,7 +276,9 @@ print("Random integer from 1 to 6:", random.randint(1, 6))
|
|
| 239 |
|
| 240 |
# Try:
|
| 241 |
# 1. Write a function is_even(n) that returns True if n is even.
|
| 242 |
-
# 2. Create a BankAccount class with deposit and withdraw methods
|
|
|
|
|
|
|
| 243 |
'''
|
| 244 |
},
|
| 245 |
{
|
|
@@ -253,8 +292,16 @@ print("Random integer from 1 to 6:", random.randint(1, 6))
|
|
| 253 |
- Topic C: Get the Contents of a Directory
|
| 254 |
- Topic D: Manage Files and Directories
|
| 255 |
|
| 256 |
-
⚠️
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
""",
|
| 259 |
"starter_code": '''# Lesson 6: Files & Directories
|
| 260 |
|
|
@@ -285,6 +332,7 @@ for item in folder.iterdir():
|
|
| 285 |
# Try:
|
| 286 |
# 1. Add another file with different text.
|
| 287 |
# 2. Read both files and print their contents.
|
|
|
|
| 288 |
'''
|
| 289 |
},
|
| 290 |
{
|
|
@@ -296,7 +344,16 @@ for item in folder.iterdir():
|
|
| 296 |
- Topic A: Handle Exceptions
|
| 297 |
- Topic B: Raise Exceptions
|
| 298 |
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
""",
|
| 301 |
"starter_code": '''# Lesson 7: Exceptions
|
| 302 |
|
|
@@ -329,12 +386,14 @@ except ValueError as e:
|
|
| 329 |
|
| 330 |
# Try:
|
| 331 |
# 1. Wrap more user input in try/except to catch ValueError.
|
| 332 |
-
# 2. Write a function that raises ValueError if a discount is
|
|
|
|
|
|
|
| 333 |
'''
|
| 334 |
},
|
| 335 |
]
|
| 336 |
|
| 337 |
-
appendix_markdown =
|
| 338 |
## 📎 References & Appendices
|
| 339 |
|
| 340 |
This tab consolidates extra reference material for the course.
|
|
@@ -387,11 +446,12 @@ Follow these conventions to keep your code clean and readable:
|
|
| 387 |
|
| 388 |
**Comments & Docstrings**
|
| 389 |
- Use `#` for short explanations above a line or block.
|
| 390 |
-
- Use triple-quoted strings
|
|
|
|
| 391 |
|
| 392 |
**Structure**
|
| 393 |
- Break large scripts into functions and classes.
|
| 394 |
-
- For larger programs, use
|
| 395 |
|
| 396 |
```python
|
| 397 |
if __name__ == "__main__":
|
|
|
|
| 45 |
- Topic C: Create a Python Application
|
| 46 |
- Topic D: Prevent Errors
|
| 47 |
|
| 48 |
+
**You’ll practice:**
|
| 49 |
+
- Using `print()` and variables
|
| 50 |
+
- Reading user input with `input()`
|
| 51 |
+
- Building a tiny interactive console app
|
| 52 |
+
|
| 53 |
+
**Mini-project idea:**
|
| 54 |
+
Create a **“Profile Card”** script that asks for your name, age, and hobby, then prints a nicely formatted profile.
|
| 55 |
""",
|
| 56 |
"starter_code": '''# Lesson 1: Simple Hello & Input
|
| 57 |
|
|
|
|
| 63 |
print(f"Hello, {name}! You are {age} years old.")
|
| 64 |
|
| 65 |
# Try:
|
| 66 |
+
# 1. Ask for a favorite color and food and add them to the message.
|
| 67 |
+
# 2. Create a small "profile card" printed with multiple lines, e.g.:
|
| 68 |
+
# Name: ...
|
| 69 |
+
# Age: ...
|
| 70 |
+
# Favorite color: ...
|
| 71 |
'''
|
| 72 |
},
|
| 73 |
{
|
|
|
|
| 79 |
- Topic A: Process Strings and Integers
|
| 80 |
- Topic B: Process Decimals, Floats, and Mixed Number Types
|
| 81 |
|
| 82 |
+
**You’ll practice:**
|
| 83 |
+
- Combining strings
|
| 84 |
+
- Doing arithmetic with integers and floats
|
| 85 |
+
- Using `round()` and basic numeric conversions
|
| 86 |
+
|
| 87 |
+
**Mini-project idea:**
|
| 88 |
+
Create a **BMI & Health Stats** script that asks for height, weight, and age, calculates BMI, and prints a short health summary.
|
| 89 |
""",
|
| 90 |
"starter_code": '''# Lesson 2: Strings, Integers, Floats
|
| 91 |
|
|
|
|
| 106 |
print("BMI (rounded to 2 decimals):", round(bmi, 2))
|
| 107 |
|
| 108 |
# Try:
|
| 109 |
+
# 1. Ask the user for their name and birth year; compute their age.
|
| 110 |
+
# 2. Convert a temperature from Fahrenheit to Celsius:
|
| 111 |
+
# celsius = (f - 32) * 5/9
|
| 112 |
+
# 3. Ask for two prices and print the subtotal, tax, and total.
|
| 113 |
'''
|
| 114 |
},
|
| 115 |
{
|
|
|
|
| 121 |
- Topic A: Process Ordered Data Structures (lists, tuples)
|
| 122 |
- Topic B: Process Unordered Data Structures (sets, dictionaries)
|
| 123 |
|
| 124 |
+
**You’ll practice:**
|
| 125 |
+
- Storing groups of values in lists and tuples
|
| 126 |
+
- Using sets to deduplicate data
|
| 127 |
+
- Using dictionaries to store labeled data
|
| 128 |
+
|
| 129 |
+
**Mini-project idea:**
|
| 130 |
+
Build a tiny **“Student Gradebook”** using a dictionary of names and scores. Compute averages and print a report.
|
| 131 |
""",
|
| 132 |
"starter_code": '''# Lesson 3: Lists, Tuples, Sets, Dictionaries
|
| 133 |
|
|
|
|
| 157 |
|
| 158 |
print("Student name:", student["name"])
|
| 159 |
|
| 160 |
+
print("\nAll student key/value pairs:")
|
| 161 |
for key, value in student.items():
|
| 162 |
print(f"{key}: {value}")
|
| 163 |
|
| 164 |
# Try:
|
| 165 |
+
# 1. Add a new key "major" to the student dictionary, then print it.
|
| 166 |
# 2. Create a list of 5 movies and print the first and last.
|
| 167 |
+
# 3. Create a "gradebook" dict: student_name -> list of scores,
|
| 168 |
+
# then print each student's average.
|
| 169 |
'''
|
| 170 |
},
|
| 171 |
{
|
|
|
|
| 177 |
- Topic A: Write a Conditional Statement (`if`, `elif`, `else`)
|
| 178 |
- Topic B: Write a Loop (`for`, `while`)
|
| 179 |
|
| 180 |
+
**You’ll practice:**
|
| 181 |
+
- Making decisions in code
|
| 182 |
+
- Repeating actions until some condition is met
|
| 183 |
+
- Combining `if` with loops for basic logic
|
| 184 |
+
|
| 185 |
+
**Mini-project idea:**
|
| 186 |
+
Create a **“Number Guessing Game”** that picks a random number and gives the user several attempts with hints (too high / too low).
|
| 187 |
""",
|
| 188 |
"starter_code": '''# Lesson 4: Conditionals & Loops
|
| 189 |
|
|
|
|
| 206 |
print(f"Your grade is: {grade}")
|
| 207 |
|
| 208 |
# Loop example: print even numbers 2-20
|
| 209 |
+
print("\nEven numbers from 2 to 20:")
|
| 210 |
for n in range(2, 21, 2):
|
| 211 |
print(n, end=" ")
|
| 212 |
|
|
|
|
| 215 |
# Try:
|
| 216 |
# 1. Use a while loop to ask for a password until it is 'secret'.
|
| 217 |
# 2. Write an if/elif/else to classify a number as positive, negative, or zero.
|
| 218 |
+
# 3. (Challenge) Make a number guessing game (1-20) that tells the user if
|
| 219 |
+
# their guess is too high or too low.
|
| 220 |
'''
|
| 221 |
},
|
| 222 |
{
|
|
|
|
| 229 |
- Topic B: Define and Instantiate a Class
|
| 230 |
- Topic C: Import and Use a Module
|
| 231 |
|
| 232 |
+
**You’ll practice:**
|
| 233 |
+
- Creating functions with parameters and return values
|
| 234 |
+
- Creating simple classes with attributes and methods
|
| 235 |
+
- Using built-in modules like `math`, `random`
|
| 236 |
+
|
| 237 |
+
**Mini-project idea:**
|
| 238 |
+
Create a **“Banking Simulator”** with a `BankAccount` class that supports deposit, withdraw, and showing balance.
|
| 239 |
""",
|
| 240 |
"starter_code": '''# Lesson 5: Functions, Classes, and Modules
|
| 241 |
|
|
|
|
| 276 |
|
| 277 |
# Try:
|
| 278 |
# 1. Write a function is_even(n) that returns True if n is even.
|
| 279 |
+
# 2. Create a BankAccount class with deposit and withdraw methods,
|
| 280 |
+
# and test it with a few transactions.
|
| 281 |
+
# 3. Use random.randint to simulate rolling two dice and print their sum.
|
| 282 |
'''
|
| 283 |
},
|
| 284 |
{
|
|
|
|
| 292 |
- Topic C: Get the Contents of a Directory
|
| 293 |
- Topic D: Manage Files and Directories
|
| 294 |
|
| 295 |
+
⚠️ In a hosted environment, file access is temporary and sandboxed,
|
| 296 |
+
but the patterns you learn here transfer directly to your local machine.
|
| 297 |
+
|
| 298 |
+
**You’ll practice:**
|
| 299 |
+
- Creating directories and files
|
| 300 |
+
- Reading and writing text data
|
| 301 |
+
- Listing directory contents
|
| 302 |
+
|
| 303 |
+
**Mini-project idea:**
|
| 304 |
+
Create a simple **“Log Writer”** that appends timestamped messages to a log file, then reads and prints the last N lines.
|
| 305 |
""",
|
| 306 |
"starter_code": '''# Lesson 6: Files & Directories
|
| 307 |
|
|
|
|
| 332 |
# Try:
|
| 333 |
# 1. Add another file with different text.
|
| 334 |
# 2. Read both files and print their contents.
|
| 335 |
+
# 3. Add a timestamp to each line you write into a 'log.txt' file.
|
| 336 |
'''
|
| 337 |
},
|
| 338 |
{
|
|
|
|
| 344 |
- Topic A: Handle Exceptions
|
| 345 |
- Topic B: Raise Exceptions
|
| 346 |
|
| 347 |
+
**You’ll practice:**
|
| 348 |
+
- Wrapping risky code in `try/except`
|
| 349 |
+
- Catching specific errors like `ValueError` and `ZeroDivisionError`
|
| 350 |
+
- Raising your own exceptions when inputs are invalid
|
| 351 |
+
|
| 352 |
+
**Mini-project idea:**
|
| 353 |
+
Create a **“Safe Calculator”** that:
|
| 354 |
+
- Accepts user input for operations
|
| 355 |
+
- Handles invalid inputs gracefully
|
| 356 |
+
- Prevents divide-by-zero errors
|
| 357 |
""",
|
| 358 |
"starter_code": '''# Lesson 7: Exceptions
|
| 359 |
|
|
|
|
| 386 |
|
| 387 |
# Try:
|
| 388 |
# 1. Wrap more user input in try/except to catch ValueError.
|
| 389 |
+
# 2. Write a function that raises ValueError if a discount is
|
| 390 |
+
# not between 0 and 100.
|
| 391 |
+
# 3. Build a small "safe calculator" that handles bad input gracefully.
|
| 392 |
'''
|
| 393 |
},
|
| 394 |
]
|
| 395 |
|
| 396 |
+
appendix_markdown = """
|
| 397 |
## 📎 References & Appendices
|
| 398 |
|
| 399 |
This tab consolidates extra reference material for the course.
|
|
|
|
| 446 |
|
| 447 |
**Comments & Docstrings**
|
| 448 |
- Use `#` for short explanations above a line or block.
|
| 449 |
+
- Use triple-quoted strings for docstrings, for example:
|
| 450 |
+
`\"\"\"This is a multi-line docstring.\"\"\"`.
|
| 451 |
|
| 452 |
**Structure**
|
| 453 |
- Break large scripts into functions and classes.
|
| 454 |
+
- For larger programs, use:
|
| 455 |
|
| 456 |
```python
|
| 457 |
if __name__ == "__main__":
|