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.
ثلاثة أنواع من الأخطاء
- خطأ في الصياغة (Syntax Error): ينتهك قواعد بايثون، لذا لن يعمل الكود.
- خطأ وقت التشغيل (Runtime Error): يتعطل البرنامج أثناء تشغيله.
- خطأ منطقي (Logic Error): يعمل بشكل صحيح لكنه يُنتج إجابة خاطئة.
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.
أخطاء الصياغة
- هذه أخطاء إملائية:缺失
:، أو علامات اقتباس غير مغلقة، أو مسافة بادئة خاطئة. - يرفض بايثون البدء ويعرض عليك السطر.
- صحح الإملاء أو علامات الترقيم، ثم شغّل مرة أخرى.
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")، أو القراءة beyond نهاية القائمة. - يتوقف البرنامج عند ذلك السطر مع رسالة خطأ.
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)
- عندما يتعطل برنامج، يطبع بايثون سجل تتبّع.
- اقرأ السطر الأخير أولاً: حيث يحدد نوع الخطأ وما الذي ذهب بشكل خاطئ.
- رقم السطر يخبرك أين تبحث.
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، لكنها تتوقف عن عدد واحد مبكرًا (خطأ منطقي بإزاحة واحد). صحّ الـ 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. · اضغط تشغيل لرؤية المخرجات هنا.