You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -21,15 +21,15 @@ Feeling a bit lost when your **Vibe Coders** agents spit out lines of python cod
21
21
22
22
23
23
---
24
-
## 1.1. 🧭 How is it explained? <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
24
+
## 🧭 1.1. How is it explained? <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
25
25
26
26
To cover all the basic Python concepts, let’s create a **mini text-based** game called **“Monster Maze”**. It’s fun, simple, and touches on all the listed topics.
27
27
28
28
You are stuck in a maze. Each turn, you decide to move through rooms, pick up items, and fight random monsters. The goal is to find the **magic key** to escape.
29
29
30
30
---
31
31
32
-
## 1.2. 🧠 What you will learn? <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
32
+
## 🧠 1.2. What you will learn? <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
@@ -61,9 +61,9 @@ You are stuck in a maze. Each turn, you decide to move through rooms, pick up it
61
61
62
62
---
63
63
64
-
###🧱 1.3. Step-by-Step Coding <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
64
+
## 🧱 1.3. Step-by-Step Coding <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
65
65
66
-
####1.3.1. Import module and Comments <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
66
+
###📦 1.3.1. Import module and Comments <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
67
67
The import statement in Python allows you to include and use code from other modules in your current program. For example the in code below:
68
68
```python
69
69
# Simple import - access with module_name.item
@@ -74,7 +74,7 @@ random_number = random.randint(1, 10) # Generates a random integer between 1 an
74
74
Python searches for a module named "random" or file named random.py in several locations and executes its code once. A namespace named "random" is created in your program and then you can access the module's functions and variables. In the example the function randint is used to create a random integer number between 1 and 10.
75
75
In python, anything written after "#" until the end of the line is interpreted as a comment and editors generally show them in green or grey.
76
76
77
-
####1.3.2. Constants and Lists <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
77
+
###📋 1.3.2. Constants and Lists <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
78
78
79
79
In Python, variables that are meant to remain unchanged throughout a program are often written in ALL_CAPS to indicate they are **constants**. While Python doesn't enforce this (variables can still be changed), it's a convention to signal to other programmers that these values shouldn't be modified.
80
80
@@ -101,7 +101,7 @@ Lists are incredibly versatile in Python:
101
101
Later in our game, we'll select random elements from these lists using `random.choice()` to create unpredictable gameplay.
102
102
103
103
104
-
####1.3.3. Global variables, Functions and Print <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
104
+
###🌐 1.3.3. Global variables, Functions and Print <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
105
105
Global variables in Python are variables that are defined outside of any function and can be accessed throughout the program, including inside functions. The Global variable found_key gets the value False at the beginning of monster_maze.py
106
106
```python
107
107
# Global variable
@@ -134,7 +134,7 @@ def print_welcome():
134
134
The string written below the function with triple """ contains a
135
135
short documentation text named **"Docstrings"** which is used to convey the purpose and functionality of Python functions, modules, and classes.
136
136
137
-
#### 1.3.4. Dictionaries and Functions with Output <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
137
+
###🔑 1.3.4. Dictionaries, Lists of Dictionaries, Tuples and Slicing <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
138
138
139
139
**Dictionaries** are one of Python's most powerful data structures. They store data as key-value pairs, allowing you to retrieve values quickly using their associated keys (similar to how you look up definitions in a real dictionary). Dictionaries are created using curly braces `{}` with each key-value pair separated by commas.
140
140
@@ -157,6 +157,79 @@ In this dictionary:
157
157
- You access values using their keys: `player["health"]` would give you `100`
158
158
- Values can be modified: `player["health"] -= 20` would reduce health by 20
159
159
160
+
**Lists of Dictionaries** are powerful data structures that can store multiple records with named fields. They're ideal for collections of similar objects.
Slicing is a concise and powerful way to manipulate sequences in Python, while lists of dictionaries and tuples provide flexible options for organizing complex data structures in your games.
230
+
231
+
### ⚙️ 1.3.5 Functions with Input and Output <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
232
+
**Functions with Input** are functions where a variable is passed as value when they are called. This is done in our code when `game_loop(player)` is called in `main()`.
160
233
**Functions with Output** are functions that return values to be used elsewhere in your code. In Python, the `return` statement is used to specify what value a function should output. Without a return statement, functions return `None` by default.
161
234
162
235
Our `create_player()` function above is a perfect example:
@@ -167,7 +240,59 @@ Our `create_player()` function above is a perfect example:
167
240
168
241
Return values are essential when a function needs to compute or create something that will be used by other parts of your program. In our game, the player dictionary is central to the entire program's state, which is why we have a dedicated function that returns it.
169
242
170
-
#### 1.3.5. Conditional Statements and String Formatting <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
243
+
**Functions with unknown input** In Python, it is possible to create a function that accepts an unknown number of arguments using `*args` and `**kwargs`. Here's a breakdown of when and why we use each:
244
+
245
+
`*args` (Arbitrary Positional Arguments): Used when you need to create a function that can operate on an unspecified number of inputs of the same type.
246
+
247
+
How it works:
248
+
- The *args syntax in a function definition collects all the extra positional arguments passed to the function into a tuple.
249
+
- The name args is a convention; you could use *whatever if you wanted, but *args is widely understood and recommended.
`**kwargs` (Arbitrary Keyword Arguments): used when you want a function to accept any number of keyword arguments (arguments passed with a `key=value` syntax).
266
+
267
+
How it works:
268
+
- The `**kwargs` syntax in a function definition collects all the extra keyword arguments passed to the function into a dictionary.
269
+
- The name kwargs is a convention; you could use `**whatever_else` but `**kwargs` is the standard.
You can combine `*args` and `**kwargs`, for example `def generic_printer(arg1, *args, **kwargs):`
293
+
294
+
295
+
### 🔀 1.3.6. Conditional Statements and String Formatting <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
171
296
172
297
**Conditional Statements** (if/elif/else) are fundamental building blocks in Python that allow your program to make decisions. They execute different code blocks based on whether certain conditions are true or false. Let's look at the `describe_room()` function as an example:
173
298
@@ -219,7 +344,7 @@ else:
219
344
The combination of these features makes our code both functional and readable. Notice how the function uses conditions to create dynamic gameplay (sometimes finding items, sometimes not) and formatted strings to clearly communicate what's happening to the player.
220
345
221
346
222
-
#### 1.3.6. Range() and Logical Operators <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
347
+
###🔢 1.3.7. Range() and Logical Operators <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
223
348
224
349
**The `range()` Function** is a built-in Python function that generates a sequence of numbers. It's commonly used in for loops to execute code a specific number of times.
#### 1.3.7. While and For Loops to Control Flow. Function Recursion <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
437
+
###🔄 1.3.8. While and For Loops to Control Flow. Function Recursion <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
313
438
314
439
Here is where we put the computer to properly work for us by using while and for loops to repeat actions. **While loops** execute a block of code repeatedly as long as a condition remains true. They're ideal when you don't know in advance how many iterations you'll need.
315
440
@@ -381,7 +506,7 @@ if choice in ["yes", "y"]:
381
506
382
507
This creates a chain of function calls that continue until a terminating condition is met (finding the key or dying). Recursion is powerful but needs a clear exit condition to avoid infinite recursion.
383
508
384
-
#### 1.3.8. Main Execution and Flow Diagram <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
509
+
###🏃 1.3.9. Main Execution and Flow Diagram <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
385
510
386
511
**Python Script Execution** follows a specific order:
387
512
@@ -468,7 +593,7 @@ graph TD;
468
593
H --> W;
469
594
```
470
595
471
-
#### 1.3.9. Debugging <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
596
+
###🐛 1.3.10. Debugging <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
472
597
473
598
**Debugging** is the process of finding and fixing errors (bugs) in your code. Common debugging techniques in Python include:
474
599
@@ -502,7 +627,7 @@ Good debugging practices:
502
627
- Use descriptive print statements
503
628
- Check edge cases (empty lists, zero values, etc.)
504
629
505
-
#### 1.3.10. Refactor and Test, Code Structure and UI Polish <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
630
+
###🔧 1.3.11. Refactor and Test, Code Structure and UI Polish <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
506
631
507
632
**Refactoring** is the process of restructuring code without changing its behavior. Benefits include:
508
633
- Improved readability
@@ -545,7 +670,7 @@ As a final step, thorough testing ensures your code works as expected across dif
545
670
546
671
---
547
672
548
-
###🎯 1.4. Exercises <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
673
+
## 🎯 1.4. Exercises <ahref="#top"class="back-to-top-link"aria-label="Back to Top">↑</a>
549
674
550
675
#### 🧪 Practice 1: Custom Weapons
551
676
> Modify the `ITEMS` list to include new weapons like "laser", "bow", or "fireball". Have the monster encounter logic recognize them.
0 commit comments