Repository navigation
Expand file tree
/
Copy pathchapter_14.js
More file actions
190 lines (190 loc) · 9.89 KB
/
Copy pathchapter_14.js
File metadata and controls
190 lines (190 loc) · 9.89 KB
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
PB.registerChapter({
version: 2,
id: 14,
title: "ماژولها",
level: "expert",
lesson: {
title: "ماژولها — کد مشترک",
intro: "هیچکس از صفر شروع نمیکنه! پایتون هزاران ماژول آماده داره — از ریاضیات تا وب. ماژولها بهت اجازه میدن کدهای آماده رو import کنی و از قدرت کل جامعهی پایتون استفاده کنی.",
sections: [
{
icon: "📦",
title: "import — آوردن کد",
text: "سه روش import:\n\nimport math — کل ماژول (math.sqrt(9))\nfrom math import sqrt — فقط یک تابع (sqrt(9))\nimport math as m — با نام کوتاه (m.sqrt(9))\n\nاولی همهچی میاره، دومی فقط لازم رو.",
code: "import math\nprint(math.sqrt(16))\nprint(math.pi)\n\nfrom math import floor\nprint(floor(3.7))\n\nfrom math import sqrt as s\nprint(s(25))",
output: "4.0\n3.141592653589793\n3\n5.0"
},
{
icon: "🎲",
title: "ماژول random",
text: "برای کار با شانس و تصادفی:\n\nrandom.randint(1, 6) — عدد تصادفی بین\nrandom.choice(list) — انتخاب از لیست\nrandom.shuffle(list) — جابهجا کردن\nrandom.random() — عدد اعشاری ۰ تا ۱",
code: "import random\n\nnames = ['علی', 'سارا', 'رضا']\nprint(random.choice(names))\nprint(random.randint(1, 6))\n\nrandom.shuffle(names)\nprint(names)",
output: ""
},
{
icon: "🕐",
title: "ماژول datetime",
text: "برای تاریخ و زمان:\n\ndatetime.datetime.now() — الان\ndatetime.date.today() — امروز\n\nو ماژول time برای تاخیر:\ntime.sleep(2) — ۲ ثانیه صبر کن",
code: "import datetime\nimport time\n\ntoday = datetime.date.today()\nprint(today.year)\nprint(today.month)\n\nprint('شروع')\ntime.sleep(1)\nprint('پایان')",
output: "2026\n8\nشروع\nپایان"
},
{
icon: "🧰",
title: "ساخت ماژول خودت",
text: "ماژول خودت هم بسازی! کافیه یه فایل .py بسازی و از جایی دیگه import کنی:\n\n# tools.py\ndef square(x):\n return x * x\n\n# main.py\nfrom tools import square\nprint(square(5))\n\nهر فایل پایتون میتونه ماژول باشه.",
code: "# tools.py\ndef square(x):\n return x * x\n\n# main.py\nfrom tools import square\nprint(square(5))",
output: "25"
}
],
tips: [
"from module import x خاصتره و کد تمیزتری میده.",
"قبل از استفاده از ماژول حتماً نصبش کن (pip install).",
"ماژولهای خودت رو توی فایلهای جدا بذار و import کن.",
]
},
exercises: [
{
id: "ch14_e1",
type: "predict",
title: "خروجی این کد چیه؟",
code: "import math\nprint(math.sqrt(36))",
answer: "6.0",
hint: "جذر 36",
hints: ["ریشه دوم 36 = 6", "sqrt همیشه float برمیگردونه"],
explanation: "math.sqrt(36) = 6.0 (ریشه دوم همیشه float برمیگردونه)."
},
{
id: "ch14_e2",
type: "predict",
title: "خروجی این کد چیه؟",
code: "from math import floor\nprint(floor(4.9))",
answer: "4",
hint: "گرد کردن به پایین",
hints: ["floor یعنی کف", "4.9 → 4"],
explanation: "floor عدد رو به پایین گرد میکنه: 4."
},
{
id: "ch14_e3",
type: "predict",
title: "خروجی این کد چیه؟",
code: "import random\n\nnums = [1, 2, 3, 4, 5]\nrandom.shuffle(nums)\nprint(len(nums))",
answer: "5",
hint: "shuffle فقط جابهجا میکنه",
hints: ["تعداد عنصرها عوض نمیشه", "هنوز ۵ تاست"],
explanation: "shuffle فقط ترتیب رو جابهجا میکنه؛ تعداد عنصرها (5) ثابت میمونه."
},
{
id: "ch14_e4",
type: "fill_gap",
title: "ماژول رو بیار:",
code: "___ math\nprint(math.pi)",
answer: "import",
hint: "کلمهی آوردن ماژول",
hints: ["import math", "import"],
explanation: "با import math میتونی از توابع و ثابتهای math استفاده کنی."
},
{
id: "ch14_e5",
type: "quiz",
title: "این یعنی چی؟",
code: "from datetime import date",
options: [
{ label: "a", text: "کل datetime میاد" },
{ label: "b", text: "فقط date از datetime میاد" },
{ label: "c", text: "کل date میاد" },
{ label: "d", text: "خطا میده" }
],
correct: "b",
hint: "from...import",
hints: ["from X import Y یعنی فقط Y از X", "فقط date"],
explanation: "from datetime import date یعنی فقط کلاس/تابع date از ماژول datetime میاد."
},
{
id: "ch14_e6",
type: "predict",
title: "خروجی این کد چیه؟",
code: "import time\nprint('الف')\ntime.sleep(1)\nprint('ب')",
answer: "الف\nب",
hint: "فقط یه تاخیر کوچیکه",
hints: ["الف چاپ میشه", "یه ثانیه صبر", "ب چاپ میشه"],
explanation: "کد الف رو چاپ میکنه، یه ثانیه صبر میکنه، بعد ب رو چاپ میکنه."
}
],
challenges: [
{
id: "ch14_c1",
type: "predict",
difficulty: "easy",
title: "خروجی این کد چیه؟",
code: "import math\nprint(math.ceil(2.1))",
answer: "3",
hint: "ceil یعنی سقف — گرد به بالا",
xp: 10,
explanation: "math.ceil عدد رو به بالا گرد میکنه: 2.1 → 3."
},
{
id: "ch14_c2",
type: "fill_gap",
difficulty: "medium",
title: "عدد تصادفی بساز:",
code: "import random\nnum = random.___(1, 10)\nprint(num)",
answer: "randint",
hint: "عدد صحیح تصادفی",
xp: 15,
explanation: "random.randint(1, 10) عدد صحیح تصادفی بین ۱ تا ۱۰ میده."
},
{
id: "ch14_c3",
type: "bug_hunter",
difficulty: "medium",
title: "کجای کد خطا داره؟",
code: "print(math.sqrt(25))",
error_line: 1,
reason: "math import نشده — NameError میگیریم چون math تعریف نشده",
hint: "قبل استفاده باید ماژول بیاد",
xp: 20,
explanation: "بدون import math، پایتون نمیدونه math چیه → NameError. اول باید import math."
},
{
id: "ch14_c4",
type: "quiz",
difficulty: "medium",
title: "یه مورد از لیست انتخاب کن:",
code: "random.choice(['a', 'b', 'c'])",
options: [
{ label: "a", text: "همیشه 'a'" },
{ label: "b", text: "یه عدد تصادفی" },
{ label: "c", text: "یه عنصر تصادفی از لیست" },
{ label: "d", text: "لیست مرتب شده" }
],
correct: "c",
hint: "choice یعنی انتخاب",
xp: 15,
explanation: "random.choice یک عنصر تصادفی از لیست برمیگردونه."
},
{
id: "ch14_c5",
type: "sort",
difficulty: "hard",
title: "مرتب کن تا سال چاپ بشه:",
code: "print(today.year)\ntoday = datetime.date.today()\nimport datetime",
correct_order: ["import datetime", "today = datetime.date.today()", "print(today.year)"],
answer: ["import datetime", "today = datetime.date.today()", "print(today.year)"],
hint: "import اول، بعد استفاده",
xp: 25,
explanation: "اول ماژول میاد، بعد تاریخ امروز، بعد چاپ سال."
}
],
project: {
id: "ch14_project",
title: "قرعهکشی",
brief: "لیستی از ۵ اسم بساز، با random.choice یه برنده انتخاب کن و با math از یه محاسبه استفاده کن. نتیجه رو چاپ کن.",
accepts: [
{ check: (c) => /import\s+random/.test(c), success: "random import کردی", hint: "import random", points: 3 },
{ check: (c) => /random\.choice|random\.randint/.test(c), success: "تصادفی استفاده کردی", hint: "با random.choice برنده رو انتخاب کن", points: 4 },
{ check: (c) => /import\s+math/.test(c), success: "math import کردی", hint: "import math هم لازمه", points: 3 },
{ check: (c) => /print/.test(c), success: "خروجی داری", hint: "نتیجه رو print کن", points: 2 }
],
passScore: 70
}
});