נבנה על ידיBuilt by Ariel Yohimovich

PythonPython
10
חלקPart 2: פונקציות וקוד אמיתיFunctions & real code כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Scope וערכי החזרהScope & return values

return מול print, משתנים מקומיים וגלובליים, ופונקציות טהורות.

return vs print, local and global variables, and pure functions.

נסו לפתור לבד לפני שמציצים. בנגן: חיצים במקלדת או הכפתורים. כל צעד מראה איפה (קובץ ושורה), מה כותבים ולמה.Try on your own before peeking. In the player use the arrow keys or the buttons. Each step shows where (file and line), what to write, and why.
תרגילExercise 1

מ-print ל-returnFrom print to return

תזכורת: מה ביקשוReminder: the task

הפונקציה square מדפיסה במקום להחזיר, ולכן החישוב בשורה האחרונה נופל. תקנו אותה כך שתחזיר, והקוד ידפיס 26.

square prints instead of returning, so the last line crashes. Fix it to return, and the code prints 26.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def square(x):
    return x * x

print(square(5) + 1)
פלטOutput
26

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 2

מינימום ומקסימוםMin and max

תזכורת: מה ביקשוReminder: the task

כתבו min_max(nums) שמחזירה טאפל (הקטן, הגדול) בלי להשתמש ב-min וב-max המובנות. אם הרשימה ריקה החזירו (None, None).

Write min_max(nums) returning a tuple (smallest, largest) without using the built-in min and max. For an empty list return (None, None).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def min_max(nums):
    if not nums:
        return None, None
    lo = hi = nums[0]
    for n in nums:
        if n < lo:
            lo = n
        if n > hi:
            hi = n
    return lo, hi

print(min_max([412, 96, 1036, 280]))
פלטOutput
(96, 1036)

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 3

בלי גלובלייםNo globals

תזכורת: מה ביקשוReminder: the task

הקוד משתמש במשתנה גלובלי ונופל עם UnboundLocalError. שכתבו את add_xp כך שתקבל את ה-XP הנוכחי ואת התוספת ותחזיר את החדש. הקוד הראשי צריך להדפיס 150.

The code uses a global and crashes with UnboundLocalError. Rewrite add_xp to take the current XP and the bonus and return the new value. The main code should print 150.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def add_xp(xp, bonus):
    return xp + bonus

xp = 50
xp = add_xp(xp, 100)
print(xp)
פלטOutput
150

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 4

עצרת ברקורסיהRecursive factorial

תזכורת: מה ביקשוReminder: the task

כתבו factorial(n) רקורסיבית: factorial(0) היא 1, ו-factorial(n) היא n * factorial(n - 1). הדפיסו את factorial(6).

Write a recursive factorial(n): factorial(0) is 1, and factorial(n) is n * factorial(n - 1). Print factorial(6).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)

print(factorial(6))
פלטOutput
720

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 5

מדף, צעד 10: חיפוש שמחזיר תוצאותShelf, step 10: a search that returns results

תזכורת: מה ביקשוReminder: the task

כתבו search(shelf, text) טהורה שמחזירה רשימה של מילוני ספרים שהכותרת או המחבר שלהם מכילים את text (בלי רגישות לאותיות גדולות). הדפיסו את הכותרות של התוצאות עבור "da".

Write a pure search(shelf, text) returning a list of book dicts whose title or author contains text (case-insensitive). Print the titles of the results for "da".

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
shelf.py
def search(shelf, text):
    t = text.lower()
    return [b for b in shelf if t in b["title"].lower() or t in b["author"].lower()]

shelf = [
    {"title": "Dune", "author": "Frank Herbert"},
    {"title": "Matilda", "author": "Roald Dahl"},
    {"title": "Emma", "author": "Jane Austen"},
]
for b in search(shelf, "da"):
    print(b["title"])
פלטOutput
Matilda

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 1

פיבונאצ'יFibonacci

תזכורת: מה ביקשוReminder: the task

כתבו fib(n) שמחזירה את מספר פיבונאצ'י ה-n (fib(0)=0, fib(1)=1). כתבו גרסה עם לולאה (לא רקורסיה, כדי שתהיה מהירה גם ל-n=80) והדפיסו את fib(80).

Write fib(n) returning the nth Fibonacci number (fib(0)=0, fib(1)=1). Use a loop (not recursion, so it's fast even for n=80) and print fib(80).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

print(fib(80))
פלטOutput
23416728348467685

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 2

סכום ספרות ברקורסיהRecursive digit sum

תזכורת: מה ביקשוReminder: the task

כתבו digit_sum(n) רקורסיבית שמחזירה את סכום הספרות של מספר שלם חיובי, בלי להמיר למחרוזת. רמז: n % 10 היא הספרה האחרונה ו-n // 10 הוא השאר.

Write a recursive digit_sum(n) returning the digit sum of a positive integer, without converting to a string. Hint: n % 10 is the last digit and n // 10 is the rest.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
def digit_sum(n):
    if n < 10:
        return n
    return n % 10 + digit_sum(n // 10)

print(digit_sum(2026))
פלטOutput
10

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

נבנה על ידיBuilt by Ariel Yohimovich