PythonPython · Chapterبەش 3 · Lectureوانە 3.3
Errors and Debuggingهەڵەکان و کرمەکوژی
The three kinds of error in Python (syntax, runtime and logic), why a syntax error stops the whole file, the most common exceptions, how to read a traceback from the bottom up, and debugging habits: printing values, trace tables and changing one thing at a time, with real bugs from the course material.
سێ جۆرەکەی لە Pythonـدا (، و )، بۆچی هەموو فایلەکە دەوەستێنێت، باوترین ، چۆن لە خوارەوە بۆ سەرەوە بخوێنیتەوە و خووەکانی : چاپکردنی ، و گۆڕینی یەک شت لە هەر جارێکدا، لەگەڵ ڕاستەقینە لە ماددەی کۆرسەکە.
What you will learnچی فێر دەبیت
- Tell syntax, runtime and logic errors apart, and say when each one is found.
- Recognise the most common exceptions and their usual causes.
- Read a traceback from the bottom up to find what went wrong and where.
- Find bugs with test inputs, temporary
print()lines and trace tables.
- ، و لێک جیا بکەیتەوە و بڵێیت هەر یەکێکیان کەی دەدۆزرێتەوە.
- باوترین و هۆکارە باوەکانیان بناسیتەوە.
- لە خوارەوە بۆ سەرەوە بخوێنیتەوە بۆ دۆزینەوەی ئەوەی چی هەڵە بوو و لە کوێ.
- بە تاقیکردنەوە، دێڕی کاتیی
print()و بدۆزیتەوە.
Everybody makes errorsهەموو کەس هەڵە دەکات
Every programmer, beginner or expert, writes code that does not work the first time. A mistake in a program is called a , and finding and fixing bugs is called . (Where these words come from is told in What is programming?; the English word family is in Technical English: programming.)
هەموو ، سەرەتاکار بێت یان شارەزا، کۆدێک دەنووسێت کە لە یەکەم جاردا کار ناکات. هەڵەیەک لە پێی دەڵێن و دۆزینەوە و چاککردنی پێی دەڵێن . (ئەوەی ئەم وشانە لە کوێوە هاتوون لە بەرنامەسازی چییە؟ گێڕدراوەتەوە؛ خێزانی وشە ئینگلیزییەکانیش لە وانەی ئینگلیزیی تەکنیکی: بەرنامەسازی هەیە.)
An error message is not a punishment. It is Python telling you what went wrong and where. This lesson teaches the three kinds of error, how to read Python's messages, and habits that find bugs quickly.
سزا نییە. Python پێت دەڵێت چی هەڵە بووە و لە کوێ. ئەم وانەیە سێ جۆرەکەی فێر دەکات، چۆن پەیامەکانی Python بخوێنیتەوە و ئەو خووانەی کە بە خێرایی دەدۆزنەوە.
Syntax errors: broken grammarهەڵەکانی داڕستن: ڕێزمانی شکاو
An error found before a program runs, because the code breaks the rules — for example a missing : in Python or a missing ; in PHP. Nothing in that file runs; Python reports SyntaxError, PHP Parse error.
کە پێش جێبەجێبوونی دەدۆزرێتەوە، چونکە کۆدەکە یاساکانی دەشکێنێت، بۆ نموونە نەبوونی : لە Pythonـدا یان ; لە PHPـدا. هیچ شتێکی ئەو فایلە جێبەجێ نابێت؛ Python دەڵێت SyntaxError و PHP دەڵێت Parse error.
A programming language has grammar rules, its . A syntax error means Python cannot understand a line, for example because of a missing colon, a missing bracket or quote, or a wrong space at the start of a line. Python names the error SyntaxError (or IndentationError, a kind of syntax error about spaces at the start of a line).
هەر یاسای ڕێزمانی هەیە، واتە . واتە Python ناتوانێت لە دێڕێک تێبگات، بۆ نموونە بەهۆی نەبوونی دوو خاڵ، کەوانە یان ، یان هەڵە لە سەرەتای دێڕدا. Python ناوی ئەم هەڵەیە دەنێت SyntaxError (یان IndentationError، جۆرێکی دەربارەی لە سەرەتای دێڕدا).
print("Start")
if 5 > 3
print("Five is bigger") File "/Users/aram/python/colon.py", line 2
if 5 > 3
^
SyntaxError: expected ':'Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Lines that start a block, such as
if,for,whileanddef, must end with:. You will meet them soon; the message is easy to recognise.ئەو دێڕانەی بلۆکێک دەست پێ دەکەن، وەک
if،for،whileوdef، دەبێت بە:کۆتاییان بێت. بەم زووانە پێیان دەگەیت؛ ناسینەوەی پەیامەکە ئاسانە.
A syntax message has four parts: the file and the line number, a copy of the line, ^ marks under the place where Python got confused, and the last line with the error type and a short explanation.
پەیامی چوار بەشی هەیە: فایل و ژمارەی دێڕ، کۆپییەکی دێڕەکە، نیشانەی ^ لە ژێر ئەو شوێنەی Python تێیدا سەری لێ شێوا و دوایین دێڕ بە جۆری هەڵەکە و ڕوونکردنەوەیەکی کورت.
The ^ shows where Python noticed the problem, which is sometimes just after the real mistake. Here the colon is missing after True, but Python points at print:
^ ئەو شوێنە پیشان دەدات کە Python کێشەکەی تێدا هەستی پێکرد، کە هەندێک جار تەنها دوای هەڵە ڕاستەقینەکەیە. لێرەدا دوو خاڵ دوای True نییە، بەڵام Python ئاماژە بە print دەکات:
while True print("Hello") File "/Users/aram/python/caret.py", line 1
while True print("Hello")
^^^^^
SyntaxError: invalid syntaxSyntax errors: broken grammarهەڵەکانی داڕستن: ڕێزمانی شکاو
Before or during the run?پێش جێبەجێکردن یان لە کاتیدا؟
Python reads the whole file and checks its syntax before it runs the first line. That is why a syntax error on line 3 stops lines 1 and 2 as well. A runtime error is different: Python only meets it when it reaches that line, so everything above it has already run. The same idea for every language, with a compiler and an interpreter side by side, is in A mistake on line 4.
Python پێش ئەوەی یەکەم دێڕ جێبەجێ بکات هەموو فایلەکە دەخوێنێتەوە و دەپشکنێت. بۆیە لە دێڕی 3 دێڕەکانی 1 و 2ـیش دەوەستێنێت. جیاوازە: Python تەنها کاتێک پێی دەگات کە دەگاتە ئەو دێڕە، بۆیە هەموو شتێکی سەرەوەی پێشتر جێبەجێ کراوە. هەمان بیرۆکە بۆ هەموو زمانێک، بە و لە تەنیشت یەکەوە، لە هەڵەیەک لە دێڕی 4دا ڕوون کراوەتەوە.
print("Line 1 runs?")
print("Line 2 runs?")
print("Line 3 has a missing quote) File "/Users/aram/python/syntax_first.py", line 3
print("Line 3 has a missing quote)
^
SyntaxError: unterminated string literal (detected at line 3)Nothing was printed: the file was rejected before it started.
هیچ چاپ نەکرا: فایلەکە پێش دەستپێکردن ڕەتکرایەوە.
print("Line 1 runs.")
print("Line 2 runs.")
print(10 / 0)
print("Line 4 never runs.")Line 1 runs.
Line 2 runs.
Traceback (most recent call last):
File "/Users/aram/python/runtime_later.py", line 3, in <module>
print(10 / 0)
~~~^~~
ZeroDivisionError: division by zeroLines 1 and 2 ran; the program stopped at line 3, so line 4 never ran.
دێڕەکانی 1 و 2 جێبەجێ کران؛ لە دێڕی 3 وەستا، بۆیە دێڕی 4 هەرگیز جێبەجێ نەکرا.
Runtime errors: exceptionsهەڵەکانی کاتی جێبەجێکردن: هەڵاوێردەکان
A happens while the program runs, in a line whose grammar is fine. Python then raises an : an error object with a name such as NameError. If nothing handles it, the program stops and Python prints a . The most common ones:
لە کاتی جێبەجێبوونی ڕوو دەدات، لە دێڕێکدا کە ڕێزمانەکەی دروستە. ئەو کاتە Python دروست دەکات: ئۆبجێکتێکی هەڵە بە ناوێکی وەک NameError. ئەگەر هیچ شتێک مامەڵەی لەگەڵ نەکات، دەوەستێت و Python چاپ دەکات. باوترینەکان:
| Exception | Exampleنموونە | Last line of the message (Python 3.12)دوایین دێڕی پەیامەکە (Python 3.12) | Usual causeهۆکاری باو |
|---|---|---|---|
NameError | print(prise) | NameError: name 'prise' is not defined. Did you mean: 'price'? | A misspelled name, or a name used before it was given a valueناوێکی هەڵە نووسراو، یان ناوێک کە پێش وەرگرتنی بەکار هاتووە |
TypeError | print("Age: " + 19) | TypeError: can only concatenate str (not "int") to str | Mixing types that do not go together, such as text + numberتێکەڵکردنی جۆرگەلێک کە پێکەوە ناگونجێن، وەک دەق + ژمارە |
ValueError | int("nineteen") | ValueError: invalid literal for int() with base 10: 'nineteen' | The right type with a wrong valueجۆری ڕاست بە هەڵە |
ZeroDivisionError | 10 / 0 | ZeroDivisionError: division by zero | Dividing by zeroدابەشکردن بەسەر سفردا |
IndexError | marks = [85, 90, 78]
print(marks[3]) | IndexError: list index out of range | Asking a list for a position it does not haveداواکردنی شوێنێک لە کە نییەتی |
UnboundLocalError | count = count + 1 inside a functioncount = count + 1 لەناو | UnboundLocalError: cannot access local variable 'count' where it is not associated with a value | Assigning to a name inside a function makes it local there, so it has no value yetدانانی بۆ ناوێک لەناو دەیکاتە ، بۆیە هێشتا نییە |
You will meet these again: NameError with variables, TypeError and ValueError with input, IndexError with lists and UnboundLocalError in Variable scope. Handling errors with try and except shows how a program can catch an exception instead of stopping.
دووبارە بەم هەڵانە دەگەیتەوە: NameError لەگەڵ گۆڕاوەکان، TypeError و ValueError لەگەڵ نێوخستە، IndexError لەگەڵ لیستەکان و UnboundLocalError لە مەودای گۆڕاوەکان. مامەڵەکردن لەگەڵ هەڵە بە try و except پیشان دەدات چۆن دەتوانێت بگرێت لە جیاتی ئەوەی بوەستێت.
price = 500
print(prise)Traceback (most recent call last):
File "/Users/aram/python/typo.py", line 2, in <module>
print(prise)
^^^^^
NameError: name 'prise' is not defined. Did you mean: 'price'?Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
A typing mistake. Since Python 3.10 the message often suggests the name you probably meant.
هەڵەیەکی نووسین. لە Python 3.10ـەوە پەیامەکە زۆرجار ئەو ناوە پێشنیار دەکات کە ڕەنگە مەبەستت بووبێت.
Reading a tracebackخوێندنەوەی ڕێچکەی هەڵە
When an exception stops a program, Python prints a : a report of the path that led to the error. It starts with Traceback (most recent call last):, which means the newest step is at the bottom. So read it from the bottom up: the last line says what went wrong; the File … line … just above it says where.
کاتێک دەوەستێنێت، Python چاپ دەکات: ڕاپۆرتێکی ئەو ڕێگەیەی گەیاندییە هەڵەکە. بە Traceback (most recent call last): دەست پێ دەکات، واتە نوێترین هەنگاو لە خوارەوەیە. بۆیە لە خوارەوە بۆ سەرەوە بیخوێنەوە: دوایین دێڕ دەڵێت چی هەڵە بوو؛ File … line …ـی ڕاستەوخۆ سەرەوەی دەڵێت لە کوێ.
def average(total, count):
return total / count
print("Average of 3 marks:", average(240, 3))
print("Average of 0 marks:", average(0, 0))Average of 3 marks: 80.0
Traceback (most recent call last):
File "/Users/aram/python/average.py", line 5, in <module>
print("Average of 0 marks:", average(0, 0))
^^^^^^^^^^^^^
File "/Users/aram/python/average.py", line 2, in average
return total / count
~~~~~~^~~~~~~
ZeroDivisionError: division by zeroExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
A small function (functions are taught in Defining and calling functions); here you only need to read the report.
بچووک ( لە ناساندن و بانگکردنی کردارەکان فێر دەکرێن)؛ لێرەدا تەنها پێویستە ڕاپۆرتەکە بخوێنیتەوە.
- What?چی؟
Last line:
ZeroDivisionError: division by zero. Something was divided by zero.دوایین دێڕ:
ZeroDivisionError: division by zero. شتێک بەسەر سفردا دابەش کراوە. - Where?لە کوێ؟
The
Fileline just above: line 2, insideaverage, the codereturn total / count. The~and^marks point at the division.دێڕی
Fileـی ڕاستەوخۆ سەرەوەی: دێڕی 2، لەناوaverage، کۆدیreturn total / count. نیشانەکانی~و^ئاماژە بە دابەشکردنەکە دەکەن. - How did we get there?چۆن گەیشتینە ئەوێ؟
The next
Fileline up: line 5 calledaverage(0, 0). Socountwas 0: the real fix is in line 5 (oraverageshould refuse a count of 0).دێڕی
Fileـی دواتر بەرەو سەرەوە: دێڕی 5average(0, 0)ـی بانگ کرد. کەواتەcount0 بوو: چاککردنی ڕاستەقینە لە دێڕی 5دایە (یانaverageدەبێت ژمارەی 0 ڕەت بکاتەوە).
Logic errors: wrong answers, no messageهەڵە لۆجیکییەکان: وەڵامی هەڵە، بەبێ پەیام
A is the hardest kind, because Python cannot see it. The code is valid and runs to the end, but it does not do what you meant. The only sign is a wrong result, so you must know what the right result is and compare.
قورسترین جۆرە، چونکە Python ناتوانێت بیبینێت. کۆدەکە دروستە و تا کۆتایی کار دەکات، بەڵام ئەوە ناکات کە مەبەستت بوو. تاکە نیشانە ئەنجامێکی هەڵەیە، بۆیە دەبێت بزانیت ئەنجامی ڕاست چییە و بەراوردی بکەیت.
a = 70
b = 80
c = 90
total = a + b
average = total / 3
print(average)50.0
The average of 70, 80 and 90 is 80.0, not 50.0. Line 4 forgot c.
تێکڕای 70، 80 و 90 دەکاتە 80.0، نەک 50.0. دێڕی 4 cـی لەبیر کردووە.
(The lines a = 70 store values in variables; they are explained in the next lesson.) Two more classic logic errors:
(دێڕەکانی وەک a = 70 لە هەڵدەگرن؛ لە وانەی داهاتوو ڕوون کراونەتەوە.) دوو کلاسیکیی تر:
a = 70
b = 90
print(a + b / 2)
print((a + b) / 2)115.0 80.0
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Division is done before addition, so this is 70 + 45. Brackets fix it (line 4). See Operators and expressions.
دابەشکردن پێش کۆکردنەوە دەکرێت، بۆیە ئەمە 70 + 45ـە. کەوانە چاکی دەکات (دێڕی 4). بڕوانە کارپێکەرەکان و دەربڕینەکان.
print("Row 1", end=" ")
print
print("Row 2")Row 1 Row 2
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
printwithout()only names the function and does nothing, so no new line is printed and the rows run together. Writeprint(). (In old Python 2,printalone did print an empty line.)printبەبێ()تەنها ناوی دەهێنێت و هیچ ناکات، بۆیە هیچ دێڕێکی نوێ چاپ ناکرێت و ڕیزەکان تێکەڵ دەبن. بنووسەprint(). (لە Python 2ـی کۆندا،printبە تەنها دێڕێکی بەتاڵی چاپ دەکرد.)
Logic errors are found by testing: run the program with inputs whose answer you already know (easy numbers, zero, the smallest and largest cases) and compare. A common family is the : a loop that runs one time too many or too few. You will practise it with loops in The for loop and range().
بە تاقیکردنەوە دەدۆزرێنەوە: بە وا جێبەجێ بکە کە پێشتر وەڵامەکەی دەزانیت (ژمارەی ئاسان، سفر، بچووکترین و گەورەترین حاڵەت) و بەراوردی بکە. خێزانێکی باو ـە: کە جارێک زیاتر یان کەمتر لە پێویست دەسوڕێتەوە. لەگەڵ لە خولی for و range() ڕاهێنانی لەسەر دەکەیت.
Debugging habits that workخووە سوودبەخشەکانی کرمەکوژی
- Read the messageپەیامەکە بخوێنەوە
Start with the last line of the traceback. Note the error type and the line number.
لە دوایین دێڕی دەست پێ بکە. جۆری هەڵەکە و ژمارەی دێڕەکە بنووسە.
- Look at the line, and the one beforeسەیری دێڕەکە و دێڕی پێشی بکە
Open that line. If it looks fine, check the end of the line above.
ئەو دێڕە بکەرەوە. ئەگەر دروست دەردەکەوێت، کۆتایی دێڕی سەرەوەی بپشکنە.
- Make the problem smallکێشەکە بچووک بکەرەوە
Run the program with the simplest input that still shows the bug, so you can predict the right answer.
بە سادەترین جێبەجێ بکە کە هێشتا پیشان دەدات، بۆ ئەوەی بتوانیت وەڵامی ڕاست پێشبینی بکەیت.
- Print the valuesنرخەکان چاپ بکە
Add a temporary
print()that shows the variables you doubt, then remove it when you are done.print()ـێکی کاتی زیاد بکە کە ئەو پیشان بدات کە گومانت لێیانە، پاشان کاتێک تەواو بوویت لای ببە. - Trace by handبە دەست بەدوای بچۆ
Fill in a trace table, or let Thonny step through the lines.
پڕ بکەرەوە، یان با Thonny هەنگاو بە هەنگاو بە دێڕەکاندا بڕوات.
- Change one thing, test againیەک شت بگۆڕە، دووبارە تاقی بکەرەوە
Fix one thing at a time and run again. Changing many things at once hides which change helped.
هەر جارە یەک شت چاک بکە و دووبارە جێبەجێی بکەرەوە. گۆڕینی چەند شتێک پێکەوە دەیشارێتەوە کام گۆڕانکاری یارمەتی دا.
- Explain it aloudبە دەنگی بەرز ڕوونی بکەرەوە
Explain your code line by line to a classmate, or even to a cup of tea. Saying each step aloud often reveals the mistake.
کۆدەکەت دێڕ بە دێڕ بۆ هاوپۆلێک ڕوون بکەرەوە، تەنانەت بۆ ئیستیکانێک چا. وتنی هەر هەنگاوێک بە دەنگی بەرز زۆرجار هەڵەکە ئاشکرا دەکات.
Language-neutral chapters: what programming is (exact instructions, like guiding a friend through a Lego set) and how to track down bugs with print statements.
دوو بەشی گشتی، بێ زمانێکی دیاریکراو: چییە ( ورد، وەک ڕێنماییکردنی هاوڕێیەک بۆ پێکەوەنانی Lego) و چۆن بە print بدۆزیتەوە.
Watch 55:22-1:00:25 ('How do we Debug Code?'): print statements and other ways to find a logic error.
لە 55:22 تا 1:00:25 سەیر بکە (How do we Debug Code?): بەکارهێنانی print و ڕێگەی تر بۆ دۆزینەوەی .
Debugging habits that workخووە سوودبەخشەکانی کرمەکوژی
Print the valuesنرخەکان چاپ بکە
The quickest habit is a temporary print(). Here it shows total in the average program from the logic-error screen:
خێراترین خوو print()ـێکی کاتییە. لێرەدا total لە تێکڕاکەی شاشەی پیشان دەدات:
a = 70
b = 80
c = 90
total = a + b
print("DEBUG total =", total)
average = total / 3
print(average)DEBUG total = 150 50.0
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
A temporary debugging line. 150 is not 70 + 80 + 90 = 240, so the bug is on line 4.
دێڕێکی کاتیی . 150 یەکسان نییە بە 70 + 80 + 90 = 240، بۆیە لە دێڕی 4دایە.
Debugging is like a doctor's work. The error message is the patient's complaint. The doctor does not guess: she asks questions, takes measurements (your print() lines and trace tables), finds the cause, gives one treatment and checks the patient again.
وەک کاری دکتۆرێکە. سکاڵای نەخۆشەکەیە. دکتۆرەکە مەزەندە ناکات: پرسیار دەکات، پێوانە وەردەگرێت (دێڕەکانی print() و )، هۆکارەکە دەدۆزێتەوە، یەک چارەسەر دەدات و دووبارە نەخۆشەکە دەپشکنێتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
A doctor cannot run the patient again from the start; you can run your program as often as you like, so test after every change.
دکتۆر ناتوانێت نەخۆشەکە لە سەرەتاوە دووبارە «جێبەجێ بکاتەوە»؛ تۆ دەتوانیت هەر چەند جار بتەوێت جێبەجێ بکەیت، بۆیە دوای هەموو گۆڕانکارییەک تاقی بکەرەوە.
Debugging habits that workخووە سوودبەخشەکانی کرمەکوژی
Trace tables: being the computerخشتەکانی بەدواداچوون: بوون بە کۆمپیوتەر
A lets you run a program by hand. Make one column for each variable and one for the output. Go through the program line by line and write down every new value. Then compare each value with what it should be: the first wrong value shows you the buggy line. Trace tables work for any algorithm, in any language; the language-free version is in Checking with a trace table.
ڕێگەت پێ دەدات بە دەست جێبەجێ بکەیت. بۆ هەر ستوونێک و بۆ ستوونێک دروست بکە. دێڕ بە دێڕ بە بڕۆ و هەموو نوێ بنووسە. پاشان هەر بەراورد بکە لەگەڵ ئەوەی دەبوو بێت: یەکەم هەڵە دێڕە باگدارەکەت پیشان دەدات. بۆ هەر و لە هەر زمانێکدا بەکار دێن؛ وەشانە گشتییەکەی بەبێ کۆد لە شاشەی پشکنین بە خشتەی بەدواداچوون هەیە.


| Lineدێڕ | a | b | c | total | average | Output |
|---|---|---|---|---|---|---|
| 1 | 70 | |||||
| 2 | 70 | 80 | ||||
| 3 | 70 | 80 | 90 | |||
| 4 | 70 | 80 | 90 | 150 (should be 240)150 (دەبوو 240 بێت) | ||
| 5 | 70 | 80 | 90 | 150 | 50.0 | |
| 6 | 70 | 80 | 90 | 150 | 50.0 | 50.0 |
Real bugs from the course materialباگی ڕاستەقینە لە ماددەی کۆرسەکەدا
The original lecture slides and lab sheets of this course contained real bugs. Each one is fixed in its own lesson; here they are side by side, so you can see the three kinds in real code.
سلاید و پەڕەکانی تاقیگەی ڕەسەنی ئەم کۆرسە ڕاستەقینەیان تێدا بوو. هەر یەکێکیان لە وانەی خۆیدا چاک کراوە؛ لێرەدا لە تەنیشت یەکەوە دانراون، بۆ ئەوەی سێ جۆرەکە لە کۆدی ڕاستەقینەدا ببینیت.
| Bug | Kindجۆر | What happensچی ڕوو دەدات | Fixed inلە کوێ چاک کراوە |
|---|---|---|---|
A multiplication table ends each row with print instead of print()خشتەیەکی لێکدان هەر ڕیزێک بە print کۆتایی پێ دەهێنێت لە جیاتی print() | Logic | No message, but all 15 numbers come out on one lineهیچ پەیامێک نییە، بەڵام هەر 15 ژمارەکە لەسەر یەک دێڕ دەردەچن | Nested loopsوانەی خولە ناوەکییەکان |
def cubeVolume(sideLength) and if sideLength >= 0 without :def cubeVolume(sideLength) و if sideLength >= 0 بەبێ : | Syntax | SyntaxError: expected ':'; nothing runsSyntaxError: expected ':'؛ هیچ جێبەجێ ناکرێت | Return valuesوانەی نرخە گەڕاوەکان |
min_position is given a value only inside an if; when the smallest number comes first it never existsmin_position تەنها لەناو ifـێکدا وەردەگرێت؛ کاتێک بچووکترین ژمارە یەکەم بێت هەرگیز دروست نابێت | Runtime | NameError: name 'min_position' is not defined. Did you mean: 'max_position'? after one line of outputNameError: name 'min_position' is not defined. Did you mean: 'max_position'? دوای یەک دێڕ | sum, max, min and sortingوانەی sum، max، min و ڕیزکردن |
| An average divided by 10 after an 11th number was added to the listتێکڕایەک بەسەر 10دا دابەش کراوە دوای ئەوەی ژمارەی 11ـەم بۆ زیاد کرا | Logic | No message; the average 152.13 is wrongهیچ پەیامێک نییە؛ تێکڕای 152.13 هەڵەیە | Adding to a list and finding itemsوانەی زیادکردن بۆ لیستێک و دۆزینەوەی توخمەکان |
An example call average(10,[2.7,32,… with one number missing and no closing )نموونە بانگکردنی average(10,[2.7,32,… کە ژمارەیەکی کەمە و )ـی داخستنی نییە | Syntax and logic و | The ) gives a SyntaxError; with it added, 9 numbers are divided by 10) SyntaxError دەدات؛ دوای زیادکردنی، 9 ژمارە بەسەر 10دا دابەش دەکرێن | Lists, references, copies and functionsوانەی لیستەکان، ئاماژەکان، کۆپی و کردارەکان |
Common debugging mistakesهەڵە باوەکانی کرمەکوژی
| Mistakeهەڵە | Betterباشتر |
|---|---|
| Reading only the top of a long tracebackتەنها خوێندنەوەی سەرەوەی درێژ | Read the last line first, then the File lines just above it.سەرەتا دوایین دێڕ بخوێنەوە، پاشان دێڕەکانی Fileـی ڕاستەوخۆ سەرەوەی. |
Changing only the exact spot under ^گۆڕینی تەنها ئەو شوێنەی لە ژێر ^ـدایە | Also check just before it and the end of the previous line.پێشیشی و کۆتایی دێڕی پێشووش بپشکنە. |
| Thinking “no error message” means “correct”وابزانیت «هیچ نییە» واتە «ڕاستە» | Test with inputs whose answer you know.بە تاقی بکەرەوە کە وەڵامەکەی دەزانیت. |
Ignoring Python's hint Did you mean: 'price'?گوێنەدان بە ئاماژەی Python Did you mean: 'price'? | The suggestion is often exactly the fix.پێشنیارەکە زۆرجار ڕێک چارەسەرەکەیە. |
| Running an old version of the fileجێبەجێکردنی وەشانێکی کۆنی فایلەکە | Save before you run; IDLE asks you to save before F5.پێش جێبەجێکردن پاشەکەوت بکە؛ IDLE پێش F5 داوای پاشەکەوتکردنت لێ دەکات. |
Leaving DEBUG prints in the finished programهێشتنەوەی printـەکانی DEBUG لە | Remove (or comment out) temporary debugging lines.دێڕە کاتییەکانی لاببە (یان بیانکە بە ). |
Copying code from the shell together with >>>کۆپیکردنی کۆد لە شێڵەوە لەگەڵ >>> | >>> is the prompt, not code; copy only what comes after it.>>> ، نەک کۆد؛ تەنها ئەوەی دوای ئەو دێت کۆپی بکە. |
Practiceڕاهێنان
Decide whether each single-line program gives a syntax error, a runtime error or a logic error. (a) print("Hi" (b) print(10 / 0) (c) print("The sum of 2 and 3 is", 2 * 3) (d) prnt("Hi") (e) print("Hi") with two spaces at the start (f) print(int("12a"))
ئەم شەش هەر یەکەیان تەنها یەک دێڕن. بڕیار بدە هەر یەکێکیان ، یان دەدات: (أ) print("Hi" (ب) print(10 / 0) (ج) print("The sum of 2 and 3 is", 2 * 3) (د) prnt("Hi") (هـ) print("Hi") بە دوو لە سەرەتادا (و) print(int("12a"))
Show answerوەڵامەکە پیشان بدە
| Program | Kindجۆر | Python 3.12 saysPython 3.12 دەڵێت |
|---|---|---|
| (a) | Syntax | SyntaxError: '(' was never closed |
| (b) | Runtime | ZeroDivisionError: division by zero |
| (c) | Logic | prints The sum of 2 and 3 is 6 (it multiplies)The sum of 2 and 3 is 6 چاپ دەکات (لێکدان دەکات) |
| (d) | Runtime | NameError: name 'prnt' is not defined. Did you mean: 'print'? |
| (e) | Syntax | IndentationError: unexpected indent |
| (f) | Runtime | ValueError: invalid literal for int() with base 10: '12a' |
Note (d): a misspelled name is valid grammar, so it is not a syntax error. Python only finds out that prnt does not exist when the line runs.
سەرنج بدە (د): ناوێکی هەڵە نووسراو ڕێزمانی دروستە، بۆیە نییە. Python تەنها کاتێک دەزانێت prnt بوونی نییە کە دێڕەکە جێبەجێ دەبێت.
Look at the traceback of average.py in Reading a traceback. Answer: (1) What is the error? (2) In which line and function did it happen? (3) Which line called that function? (4) What would you change?
سەیری average.py بکە لە خوێندنەوەی ڕێچکەی هەڵە. وەڵام بدەرەوە: (1) هەڵەکە چییە؟ (2) لە کام دێڕ و کام ڕووی دا؟ (3) کام دێڕ ئەو کردارەی بانگ کرد؟ (4) چی دەگۆڕیت؟
Show answerوەڵامەکە پیشان بدە
- (1)
ZeroDivisionError: division by zero. - (2) Line 2, inside the function
average:return total / count. - (3) Line 5:
average(0, 0), socountwas 0. - (4) Do not ask for the average of zero marks: remove or change line 5. (Later you will also learn to check
countbefore dividing.)
- (1) هەڵەکە:
ZeroDivisionError: division by zero. - (2) دێڕی 2، لەناو
average:return total / count. - (3) دێڕی 5:
average(0, 0)، کەواتەcount0 بوو. - (4) داوای تێکڕای سفر نمرە مەکە: دێڕی 5 لاببە یان بیگۆڕە. (دواتر فێر دەبیت پێش دابەشکردن
countبپشکنیت.)
Three glasses cost 2000 dinars each, so the total should be 6000. This program prints Total: 2003. Fill in a trace table and find the bug.
سێ ئیستیکان چا هەر یەکەی 2000 دینارە، بۆیە کۆی گشتی دەبێت 6000 بێت. ئەم بەرنامەیە Total: 2003 چاپ دەکات. پڕ بکەرەوە و بدۆزەرەوە.
price = 2000
count = 3
total = price + count
print("Total:", total)Total: 2003
Show answerوەڵامەکە پیشان بدە
| Lineدێڕ | price | count | total | Output |
|---|---|---|---|---|
| 1 | 2000 | |||
| 2 | 2000 | 3 | ||
| 3 | 2000 | 3 | 2003 (should be 6000)2003 (دەبوو 6000 بێت) | |
| 4 | 2000 | 3 | 2003 | Total: 2003 |
Line 3 adds instead of multiplying. Fixed:
دێڕی 3 کۆ دەکاتەوە لە جیاتی لێکدان. چاککراو:
price = 2000
count = 3
total = price * count
print("Total:", total)Total: 6000
This program should print the average of two marks (80.0) and the second mark. It contains one syntax error, one runtime error and one logic error. Fix them one at a time, running after each fix.
ئەم بەرنامەیە دەبێت تێکڕای دوو نمرە (80.0) و نمرەی دووەم چاپ بکات. یەک ، یەک و یەک تێدایە. یەک یەک چاکیان بکە و دوای هەر چاککردنێک جێبەجێی بکەرەوە.
mark1 = 70
mark2 = 90
average = mark1 + mark2 / 2
print("Average:" average)
print("Second mark:", mak2)Hintئاماژە
Python reports the syntax error first, even though it is on line 4.
Python سەرەتا ڕادەگەیەنێت، هەرچەندە لە دێڕی 4دایە.
Show answerوەڵامەکە پیشان بدە
Run 1:
SyntaxError: invalid syntax. Perhaps you forgot a comma?on line 4. Add the comma:print("Average:", average).جێبەجێکردنی 1:
SyntaxError: invalid syntax. Perhaps you forgot a comma?لە دێڕی 4. کۆماکە زیاد بکە:print("Average:", average).Run 2: it prints
Average: 115.0, thenNameError: name 'mak2' is not defined. Did you mean: 'mark2'?on line 5. Writemark2.جێبەجێکردنی 2:
Average: 115.0چاپ دەکات، پاشانNameError: name 'mak2' is not defined. Did you mean: 'mark2'?لە دێڕی 5. بنووسەmark2.Run 3: no error, but
Average: 115.0is wrong (70 + 90 / 2). Add brackets:(mark1 + mark2) / 2.جێبەجێکردنی 3: هیچ هەڵەیەک نییە، بەڵام
Average: 115.0هەڵەیە (70 + 90 / 2). کەوانە زیاد بکە:(mark1 + mark2) / 2.
mark1 = 70
mark2 = 90
average = (mark1 + mark2) / 2
print("Average:", average)
print("Second mark:", mark2)Average: 80.0 Second mark: 90
Summaryپوختە
- Syntax errors break the grammar; Python finds them before running, so nothing runs.
- Runtime errors (exceptions) happen while running: the lines before them ran, then a traceback appears. Common ones: NameError, TypeError, ValueError, ZeroDivisionError, IndexError, UnboundLocalError.
- Logic errors give wrong results with no message; only testing against known answers finds them.
- Read a traceback from the bottom: the last line says what, the
File … line …above it says where. - Debug calmly: read, look, shrink the problem, print values, trace by hand, change one thing at a time.
- ڕێزمان دەشکێنن؛ Python پێش جێبەجێکردن دەیاندۆزێتەوە، بۆیە هیچ جێبەجێ ناکرێت.
- () لە کاتی جێبەجێکردندا ڕوو دەدەن: دێڕەکانی پێشیان جێبەجێ کران، پاشان دەردەکەوێت. باوەکان: NameError، TypeError، ValueError، ZeroDivisionError، IndexError، UnboundLocalError.
- ئەنجامی هەڵە دەدەن بەبێ پەیام؛ تەنها تاقیکردنەوە بە وەڵامی زانراو دەیاندۆزێتەوە.
- لە خوارەوە بخوێنەوە: دوایین دێڕ دەڵێت چی،
File … line …ـی سەرەوەی دەڵێت لە کوێ. - بە هێمنی بکە: بخوێنەوە، سەیر بکە، کێشەکە بچووک بکەرەوە، چاپ بکە، بە دەست بەدوای بچۆ، هەر جارە یەک شت بگۆڕە.
Why did a file with a syntax error on its last line print nothing?بۆچی فایلێک کە هەڵەی داڕستن لە دوایین دێڕیدا بوو هیچی چاپ نەکرد؟
Python checks the whole file before running it; with a syntax error anywhere, no line runs.
Python پێش جێبەجێکردن هەموو فایلەکە دەپشکنێت؛ ئەگەر لە هەر شوێنێک هەبێت، هیچ دێڕێک جێبەجێ ناکرێت.
Is a misspelled function name (prnt) a syntax error?ئایا ناوێکی هەڵە نووسراوی کردار (prnt) هەڵەی داڕستنە؟
No. It is valid grammar; it raises a NameError when the line runs.
نەخێر. ڕێزمانی دروستە؛ کاتێک دێڕەکە جێبەجێ دەبێت NameError دروست دەکات.
What is a trace table?خشتەی بەدواداچوون چییە؟
A table with a column per variable and one for the output, filled in line by line to follow a program by hand.
خشتەیەکە بە ستوونێک بۆ هەر و یەکێک بۆ ، کە دێڕ بە دێڕ پڕ دەکرێتەوە بۆ بەدواداچوونی بە دەست.
How do you find a logic error?چۆن هەڵەی لۆجیکی دەدۆزیتەوە؟
Know the right answer for a simple input, run the program, compare, and trace or print values to find the first wrong one.
وەڵامی ڕاست بۆ سادە بزانە، جێبەجێ بکە، بەراوردی بکە و بەدوای بچۆ یان چاپ بکە بۆ دۆزینەوەی یەکەم هەڵە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Variables and Data Typesگۆڕاوەکان و جۆرەکانی دراوە
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python tutorial 8: Errors and Exceptions (syntax errors, exceptions)
- Python documentation: Built-in Exceptions
- Python Programming FAQ: Why am I getting an UnboundLocalError?
- Harvard CS50P, lecture 0 notes (bugs)
- Python documentation: IDLE (Run Module checks syntax; Debug menu)
- Thonny: Python IDE for beginners (variables view, step-by-step debugger)
- Liang, Introduction to Programming Using Python: table of contents (chapter 1 programming errors, chapter 13 exceptions)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.