Putting it all together · לחבר הכל יחד
Putting it all together
- You now know variables, input,
if, loops, lists, functions, and dictionaries. - A real program combines these small pieces into one useful tool.
- In this lesson you build a tiny class-scores analyzer, one function at a time.
איחוד הכל יחד
- עכשיו ידעת משתנים, כניסת נתונים,
if, לולאות, רשימות, פונקציות ומילונים. - תוכנית אמיתית משלבת חלקים קטנים אלו בכלי שימושי אחד.
- בשיעור זה תבנה analYZer נקודות כיתה זעיר, פונקציה אחת באחד.
The plan
- We have a list of test scores, like
[70, 90, 50, 80]. - We want three answers: the average, how many passed, and a short summary.
- We will write one small function for each job, then they work together.
התוכנית
- יש לנו רשימה של נקודות מבחן, כמו
[70, 90, 50, 80]. - אנחנו רוצים שלושה תשובות: הממוצע, כמה עברו, וסיכום קצר.
- נכתוב פונקציה קטנה אחת לכל משימה, ואז הן יעבודו יחד.
The analyzer calls each helper · האנלייזר קורא כל פונקציית עזר בנפרד
A big program calls small functions in turn, each returning one answer. · תוכנית גדולה קוראת לפונקציות קטנות בתור, כל אחת מחזירה תשובה אחת.
Handy built-ins for lists
- Python gives you ready-made helpers that work on a list of numbers.
len(x)counts the items,sum(x)adds them,max(x)/min(x)find the largest / smallest.- Use them instead of writing a loop every time.
פונקציות מובנות נוחות לרשימות
- Python מספק לך עוזרים מוכנים לעבודה על רשימת מספרים.
len(x)סופר פריטים,sum(x)מחשב אותם,max(x)/min(x)מוצאים את הגדול/הקטן ביותר.- השתמש בהם במקום לכתוב לולאה בכל פעם.
scores = [70, 90, 50, 80]
print(len(scores)) # 4
print(sum(scores)) # 290
print(max(scores)) # 90
print(min(scores)) # 50
A pattern you will reuse: count items that pass a test
- A very common job: loop a list, and count the items that pass some test.
- Start a counter at
0, and add1each time theifis true. - Here we count the long words; soon you will count the passing scores.
דגם שתשתמש בו שוב: ספירת פריטים שעברו מבחן
- תפקיד נפוץ מאוד: לעבור על רשימה ולספור את הפריטים העוברים מבחן מסוים.
- התחל סופר ב
0, והוסף1בכל פעם שהifמתקיים. - כאן אנו סופרים את המילים הארוכות; בקרוב תספור את הציונים העוברים.
def how_many_long_words(words, min_length):
count = 0
for w in words:
if len(w) >= min_length:
count = count + 1
return count
print(how_many_long_words(["hi", "hello", "hey", "howdy"], 4)) # 2
Common mistakes
- Build in small pieces and test each one before adding the next.
- Read the whole task first, then plan before you code.
טעויות נפוצות
- בנה קודם-בכפצות ובדוק כל שלב לפני שמוסיף את הבא.
- קרא את כל המשימה תחילה, ואז תכנן לפני שתתחיל לכתוב קוד.
Now you try
- Build the analyzer in three steps:
average,count_passing, thenstats. - Each function is small. Together they turn a list of scores into real answers.
- Press Check answer after each one.
כעת תנסו בעצמכם
- בנה את המנתח בשלושה שלבים:
average,count_passing, ולאחר מכןstats. - כל פונקציה היא קטנה. יחד הן ממירות רשימת ציונים לתשובות אמיתיות.
- לחץ על בדוק תשובה לאחר כל אחת מהן.
Write average(scores) that returns the average (mean) of a list of numbers: the total divided by how many. If the list is empty, return 0 (so it does not crash). Example: average([2, 4]) is 3.0. · כתוב average(scores) שתחזיר את הממוצע (ממוצע) של רשימת מספרים: הסכום כולו חלוקה למספר האלמנטים. אם הרשימה ריקה, החזר ⟨0⟩ (כדי שלא תקרוס). דוגמה: ⟨average([2, 4])⟩ היא ⟨3.0⟩.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write count_passing(scores, pass_mark) that returns how many scores are greater than or equal to pass_mark. Loop the list and count. Example: count_passing([40, 50, 60], 50) is 2. · כתוב count_passing(scores, pass_mark) שתחזיר כמה ציונים גדולים או שווים ל-pass_mark. עבור על הרשימה וחשב. דוגמה: ⟨count_passing([40, 50, 60], 50)⟩ היא ⟨2⟩.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write stats(scores) (assume at least one score) that returns a dictionary with three keys: "highest", "lowest", and "average". Example: stats([70, 90, 50, 80]) returns {"highest": 90, "lowest": 50, "average": 72.5}. · כתוב stats(scores) (תניחה של לפחות ציון אחד) שתחזיר מילון עם שלושה מקשרים: ⟨"highest"⟩, ⟨"lowest"⟩, ו-"average". דוגמה: ⟨stats([70, 90, 50, 80])⟩ מחזירה ⟨{"highest": 90, "lowest": 50, "average": 72.5}⟩.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write grade_counts(scores) that returns a dictionary counting grades: A for 80 and above, B for 60 and above, C for the rest. Always include all three keys: grade_counts([85, 72, 90, 40]) is {"A": 2, "B": 1, "C": 1}. · כתוב grade_counts(scores) שתחזיר מילון המונה ציונים: ⟨A⟩ עבור ⟨80⟩ ומעלה, ⟨B⟩ עבור ⟨60⟩ ומעלה, ⟨C⟩ לכל השאר. תכלל תמיד את כל שלושת המקשרים: ⟨grade_counts([85, 72, 90, 40])⟩ היא ⟨{"A": 2, "B": 1, "C": 1}⟩.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.