Errors & testing · שגיאות ובדיקות
Three kinds of error
- A syntax error breaks the rules of Python, so the code will not run.
- A runtime error crashes the program while it is running.
- A logic error runs fine but gives the wrong answer.
שלושה סוגים של טעויות
- שגיאת סינטקס מפרה את חוקי הפיתוח של Python, ולכן הקוד לא יעבוד.
- שגיאת ריצה גורמת לקריסת התוכנה בזמן שהיא פועלת.
- שגיאת לוגיקה תוכנה עובדת כראוי אך מחזירה תוצאה שגויה.
Syntax errors
- These are typos: a missing
:, an unclosed quote, a wrong indent. - Python refuses to start and shows you the line.
- Fix the spelling or punctuation, then run again.
שגיאות סינטקס
- אלו טעויות כתיב:
:חסר, ציטוט סגור לא נכון, או קידוד שגוי. - Python מסרבת להתחיל ומציגה לך את השורה.
- תיקן את הכתיב או הניקוד, ואז הרץ שוב.
Runtime errors
- The code starts, then hits something it cannot do.
- Examples:
10 / 0, orint("hello"), or reading past the end of a list. - The program stops at that line with a message.
שגיאות ריצה
- הקוד מתחיל, אך נתקל במשהו שהוא אינו יכול לבצע.
- דוגמאות:
10 / 0, אוint("hello"), או קריאה מעבר לסוף של רשימה. - התוכנה עצומה בשורה זו עם הודעה.
Logic errors
- The program runs to the end but the result is wrong.
- Example: using
-where you meant+, or stopping a loop too early. - These are the hardest to find — the computer did exactly what you wrote.
שגיאות לוגיקה
- התוכנה רצה עד הסוף אך התוצאה היא שגויה.
- דוגמה: שימוש ב-
-במקום שהתכוונת ל-+, או עצירת לולאה מוקדי מדי. - אלו הקשות ביותר לאיתור — המחשב ביצע בדיוק מה שכתבת.
Read the traceback
- When a program crashes, Python prints a traceback.
- Read the last line first: it names the error and what went wrong.
- The line number tells you where to look.
קרא את דוח השגיאה (traceback)
- כאשר תוכנה קורסת, Python מדפיסה דוח שגיאה (traceback).
- קרא תחילה את שורה האחרונה: היא מזהה את סוג השגיאה ומה הלך שגwrong.
- מספר השורה מראה לך איפה לחפש.
Traceback (most recent call last):
File "<program>", line 2, in <module>
print(10 / 0)
ZeroDivisionError: division by zero
Test with chosen data
- Do not trust code until you have tested it.
- Use normal data, boundary data (like
0), and extreme data. - Check each answer against what you worked out by hand.
בדיקה עם נתונים שנבחרו
- אל תסתמך על קוד עד שתבצע בו בדיקה.
- השתמש בנתונים רגילים, נתוני גבול (כמו
0), ונתונים קיצוניים. - בדוק כל תשובה מול מה שחישבת ידנית.
def square(n):
return n * n
print(square(3)) # normal -> 9
print(square(0)) # boundary -> 0
print(square(-2)) # negative -> 4
Common mistakes
- A syntax error stops the program before it runs, so fix those first.
- Catch a specific error with
except ValueError:, not a bareexcept:. - Test the tricky cases (empty, zero, the biggest), not just the easy one.
טעויות נפוצות
- שגיאת סינטקס עצרה את התוכנית לפני ההפעלה, לכן תרם אותן תחילה.
- תפוס שגיאה ספציפית עם
except ValueError:, ולאexcept:ריק. - בדיק מקרים מורכבים (ריק, אפס, הגדול ביותר), לא רק הקל.
Now you try
- Each task has a logic error to fix, or a crash to prevent.
- Press Check answer to test your fix against several cases.
כעת תנסו בעצמכם
- לכל משימה יש שגיאת לוגיקה לתקן, או קריסה למנוע.
- לחץ על בדוק תשובה כדי לבדוק את התיקון מול מספר מקרים.
Find and fix a bug · מצא ותקן באג
Testing is a cycle: run it, see what's wrong, fix, run again. · בדיקה היא מחזור: הרץ, ראה מה לא נכון, תקן, הרץ שוב.
This function should add a and b, but it has a logic error — it subtracts. Fix it so it returns the sum. · פונקציה זו אמורה להוסיף a ו-b, אך יש בה שגיאת לוגיקה — היא מחסרת. תקן אות כך שתחזיר את הסכום.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
sum_to(n) should return 1 + 2 + ... + n, but it stops one number too early (an off-by-one logic error). Fix the range so it includes n. · sum_to(n) אמורה להחזיר 1 + 2 + ... + n, אך היא עצירה מספר אחד מוקדם מדי (שגיאת לוגיקה של off-by-one). תקן את ה-range כדי שתכלול גם את n.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write safe_divide(a, b) that returns a / b, but prevents a runtime crash: if b is 0, return 0 instead. · כתוב safe_divide(a, b) שמחזיר a / b, אך מונעת התמוטות בזמן ריצה: אם b היא 0, החזיר 0 במקום.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
average(nums) has two bugs: it divides by the wrong number, and it crashes on an empty list. Fix both, so average([2, 4, 6]) is 4 and average([]) is 0. · average(nums) יש לו שני באגים: הוא מחלק במספר הלא נכון, והוא קורס ברשימה ריקה. תקן את שניהם, כך ש-average([2, 4, 6]) יהיה ⟨4⟩ ו-average([]) יהיה ⟨0⟩.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.