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

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

דיבאג עם VS CodeDebugging with VS Code

נקודות עצירה, צעד-אחר-צעד, צפייה במשתנים ושיטה למציאת באגים.

Breakpoints, stepping, watching variables and a method for finding bugs.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. מה עושה F10 בדיבאגר של VS Code?What does F10 do in VS Code's debugger?

2. מה ידפיס print(f"{x=}") כש-x הוא 5?What does print(f"{x=}") print when x is 5?

3. מה הבאג?What's the bug?
python
def total(nums):
    t = 0
    for n in nums:
        t += n
        return t

תרגיליםExercises

תרגילExercise 1

תקנו את הממוצעFix the average

הפונקציה מחזירה ממוצע שגוי. מצאו את הבאג (אפשר עם הדפסות) ותקנו. average([10, 20, 30]) צריך להיות 20.

The function returns a wrong average. Find the bug (prints help) and fix it. average([10, 20, 30]) should be 20.

איך הפלט אמור להיראות?What should the output look like?
20.0
main.py
רמזHint 1: הדפיסו את total ואת len(nums).Print total and len(nums).
רמזHint 2: החילוק צריך להיות במספר הפריטים, לא באחד פחות.Divide by the number of items, not one less.
לפתרוןSolution
תרגילExercise 2

הספר שלא נמצאThe book that isn't found

החיפוש לא מוצא את dune אף שהספר קיים. השתמשו ב-repr כדי לראות למה, ותקנו כך ש-find("dune") תחזיר True.

The search doesn't find dune although the book exists. Use repr to see why, and fix it so find("dune") returns True.

איך הפלט אמור להיראות?What should the output look like?
True
main.py
רמזHint 1: print(repr(b)) בתוך הלולאה.print(repr(b)) inside the loop.
רמזHint 2: גם רווחים וגם אותיות גדולות.Both spaces and capital letters.
לפתרוןSolution
תרגילExercise 3

שלושה באגיםThree bugs

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

The function should return a list of even numbers from 1 to n (inclusive). It has three bugs. Fix them all.

איך הפלט אמור להיראות?What should the output look like?
[2, 4, 6, 8, 10]
main.py
רמזHint 1: בדקו את ה-range: האם n נכלל?Check the range: is n included?
רמזHint 2: בדקו את התנאי ואת מיקום ה-return.Check the condition and where the return is.
לפתרוןSolution
תרגילExercise 4

assert שמגןA guarding assert

הוסיפו ל-discount(price, percent) שני assert: המחיר חיובי, והאחוז בין 0 ל-100 (עם הודעות). הפונקציה מחזירה את המחיר אחרי הנחה, מעוגל ל-2.

Add two asserts to discount(price, percent): the price is positive, and percent is 0 to 100 (with messages). It returns the price after the discount, rounded to 2.

איך הפלט אמור להיראות?What should the output look like?
60.0
main.py
רמזHint 1: assert price > 0, "..."assert price > 0, "..."
רמזHint 2: round(price * (1 - percent / 100), 2)round(price * (1 - percent / 100), 2)
לפתרוןSolution
תרגילExercise 5

מדף, צעד 14: באג במדףShelf, step 14: a shelf bug

פרויקט מלווהCompanion project

בגרסה הזו של המדף, מחיקת ספר מוחקת לפעמים ספר אחר, והספירה שגויה. מצאו ותקנו את שני הבאגים כך ש-remove_book תמחק לפי כותרת ו-count תחזיר את המספר הנכון.

In this shelf version, removing a book sometimes removes a different one, and the count is wrong. Find and fix both bugs so remove_book removes by title and count returns the right number.

איך הפלט אמור להיראות?What should the output look like?
['Dune', 'Matilda'] 2
shelf.py
רמזHint 1: הדפיסו את האינדקס ואת הספר שנמחק.Print the index and the book being removed.
רמזHint 2: enumerate מתחיל ב-0 כברירת מחדל.enumerate starts at 0 by default.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

חיפוש בינארי שבורA broken binary search

החיפוש הבינארי (נלמד לעומק במפגש 34) נתקע או מפספס את הפריט האחרון. תקנו אותו כך שיחזיר את האינדקס של target ברשימה ממוינת, או -1.

This binary search (covered in session 34) hangs or misses the last item. Fix it to return target's index in a sorted list, or -1.

איך הפלט אמור להיראות?What should the output look like?
4
main.py
רמזHint 1: התנאי צריך להיות lo <= hi.The condition should be lo <= hi.
רמזHint 2: אחרי השוואה מזיזים ל-mid + 1 או mid - 1, לא ל-mid.After comparing move to mid + 1 or mid - 1, not to mid.
לפתרוןSolution
אתגרChallenge 2

רישום ביומןLogging it

כתבו process(orders) שעוברת על מחירים: מחיר שלילי נרשם כ-WARNING ומדולג, מחיר 0 נרשם כ-INFO ומדולג, והשאר נסכמים. הגדירו logging לרמת INFO עם הפורמט %(levelname)s: %(message)s ל-stdout, והדפיסו את הסכום.

Write process(orders) over prices: a negative price logs a WARNING and is skipped, 0 logs INFO and is skipped, the rest are summed. Configure logging at INFO with format %(levelname)s: %(message)s to stdout, and print the total.

איך הפלט אמור להיראות?What should the output look like?
WARNING: negative price: -5
INFO: free item skipped
100
main.py
רמזHint 1: logging.basicConfig(level=logging.INFO, format=..., stream=sys.stdout, force=True)logging.basicConfig(level=logging.INFO, format=..., stream=sys.stdout, force=True)
רמזHint 2: log.warning(f"negative price: {p}")log.warning(f"negative price: {p}")
לפתרוןSolution

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