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ـدا (، و )، بۆچی هەموو فایلەکە دەوەستێنێت، باوترین ، چۆن لە خوارەوە بۆ سەرەوە بخوێنیتەوە و خووەکانی : چاپکردنی ، و گۆڕینی یەک شت لە هەر جارێکدا، لەگەڵ ڕاستەقینە لە ماددەی کۆرسەکە.

15 min read16 خولەک خوێندنەوە · 13 stepsهەنگاو

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 بخوێنیتەوە و ئەو خووانەی کە بە خێرایی دەدۆزنەوە.

A chart with three columns. Syntax error: found before the program runs; Python shows SyntaxError and nothing runs; example print("Hi" with a missing parenthesis. Runtime error (exception): found while the program runs; the lines before it ran, then a traceback appears; example 10 / 0. Logic error: the program runs to the end with no message but the answer is wrong; example an average that forgets one mark.چارتێک بە سێ ستوون. هەڵەی داڕستن: پێش جێبەجێکردنی بەرنامەکە دەدۆزرێتەوە؛ Python SyntaxError پیشان دەدات و هیچ جێبەجێ ناکرێت؛ نموونە print("Hi" کە کەوانەیەکی کەمە. هەڵەی کاتی جێبەجێکردن (هەڵاوێردە): لە کاتی جێبەجێکردندا دەدۆزرێتەوە؛ دێڕەکانی پێشی جێبەجێ کراون، پاشان ڕێچکەی هەڵە دەردەکەوێت؛ نموونە 10 / 0. هەڵەی لۆجیکی: بەرنامەکە تا کۆتایی کار دەکات بەبێ هیچ پەیامێک بەڵام وەڵامەکە هەڵەیە؛ نموونە تێکڕایەک کە نمرەیەکی لەبیر کردووە.
The three kinds of error and when you meet themسێ جۆرەکەی و کەی پێیان دەگەیتCêIT
Quick checkپشکنینی خێراA program runs to the end without any message, but prints 50.0 when the right answer is 80.0. What kind of error is this? تا کۆتایی کار دەکات بەبێ هیچ پەیامێک، بەڵام ⁦50.0⁩ چاپ دەکات لە کاتێکدا وەڵامی ڕاست ⁦80.0⁩ـە. ئەمە چ جۆرە ؟

Syntax errors: broken grammarهەڵەکانی داڕستن: ڕێزمانی شکاو

Definitionپێناسە

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، جۆرێکی دەربارەی لە سەرەتای دێڕدا).

colon.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Start")
if 5 > 3
    print("Five is bigger")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Lines that start a block, such as if, for, while and def, 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 دەکات:

caret.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
while True print("Hello")
Quick checkپشکنینی خێراWhich of these lines gives a SyntaxError?کام لەم دێڕانە SyntaxError دەدات؟

Syntax 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دا ڕوون کراوەتەوە.

syntax_first.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Line 1 runs?")
print("Line 2 runs?")
print("Line 3 has a missing quote)
runtime_later.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Line 1 runs.")
print("Line 2 runs.")
print(10 / 0)
print("Line 4 never runs.")
Quick checkپشکنینی خێراA program prints two lines and then shows a traceback. What kind of error stopped it? دوو دێڕ چاپ دەکات و پاشان پیشان دەدات. چ جۆرە ڕایگرت؟

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 چاپ دەکات. باوترینەکان:

Common exceptions
ExceptionExampleنموونەLast line of the message (Python 3.12)دوایین دێڕی پەیامەکە ⁦(Python 3.12)⁩Usual causeهۆکاری باو
NameErrorprint(prise)NameError: name 'prise' is not defined. Did you mean: 'price'?A misspelled name, or a name used before it was given a valueناوێکی هەڵە نووسراو، یان ناوێک کە پێش وەرگرتنی بەکار هاتووە
TypeErrorprint("Age: " + 19)TypeError: can only concatenate str (not "int") to strMixing types that do not go together, such as text + numberتێکەڵکردنی جۆرگەلێک کە پێکەوە ناگونجێن، وەک دەق + ژمارە
ValueErrorint("nineteen")ValueError: invalid literal for int() with base 10: 'nineteen'The right type with a wrong valueجۆری ڕاست بە هەڵە
ZeroDivisionError10 / 0ZeroDivisionError: division by zeroDividing by zeroدابەشکردن بەسەر سفردا
IndexErrormarks = [85, 90, 78] print(marks[3])IndexError: list index out of rangeAsking a list for a position it does not haveداواکردنی شوێنێک لە کە نییەتی
UnboundLocalErrorcount = count + 1 inside a functioncount = count + 1 لەناو UnboundLocalError: cannot access local variable 'count' where it is not associated with a valueAssigning 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 پیشان دەدات چۆن دەتوانێت بگرێت لە جیاتی ئەوەی بوەستێت.

typo.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
price = 500
print(prise)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

A typing mistake. Since Python 3.10 the message often suggests the name you probably meant.

هەڵەیەکی نووسین. لە ⁦Python 3.10⁩ـەوە پەیامەکە زۆرجار ئەو ناوە پێشنیار دەکات کە ڕەنگە مەبەستت بووبێت.

Quick checkپشکنینی خێراWhich exception does int("12a") raise?int("12a") کام دروست دەکات؟

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 …ـی ڕاستەوخۆ سەرەوەی دەڵێت لە کوێ.

average.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
def average(total, count):
    return total / count

print("Average of 3 marks:", average(240, 3))
print("Average of 0 marks:", average(0, 0))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

A small function (functions are taught in Defining and calling functions); here you only need to read the report.

بچووک ( لە ناساندن و بانگکردنی کردارەکان فێر دەکرێن)؛ لێرەدا تەنها پێویستە ڕاپۆرتەکە بخوێنیتەوە.

The traceback of average.py with labels read from the bottom up. Bottom line: ZeroDivisionError: division by zero, labelled what went wrong, read this first. Above it: File average.py, line 2, in average, with the code return total / count and ~ and ^ marks under the division, labelled where it failed. Above that: File average.py, line 5, in <module>, the call average(0, 0), labelled how the program got there. Top: Traceback (most recent call last).ڕێچکەی هەڵەی average.py لەگەڵ ناونیشان، کە لە خوارەوە بۆ سەرەوە دەخوێندرێتەوە. دێڕی خوارەوە: ZeroDivisionError: division by zero، بە ناونیشانی «چی هەڵە بوو، سەرەتا ئەمە بخوێنەوە». سەرەوەی: File average.py، line 2، in average، لەگەڵ کۆدی return total / count و نیشانەی ~ و ^ لە ژێر دابەشکردنەکە، بە ناونیشانی «لە کوێ شکستی هێنا». سەرەوەتر: File average.py، line 5، in <module>، بانگکردنی average(0, 0)، بە ناونیشانی «بەرنامەکە چۆن گەیشتە ئەوێ». سەرەوە: Traceback (most recent call last).
Read a traceback from the bottom up. لە خوارەوە بۆ سەرەوە بخوێنەوە.CêIT
  1. What?چی؟

    Last line: ZeroDivisionError: division by zero. Something was divided by zero.

    دوایین دێڕ: ZeroDivisionError: division by zero. شتێک بەسەر سفردا دابەش کراوە.

  2. Where?لە کوێ؟

    The File line just above: line 2, inside average, the code return total / count. The ~ and ^ marks point at the division.

    دێڕی Fileـی ڕاستەوخۆ سەرەوەی: دێڕی 2، لەناو average، کۆدی return total / count. نیشانەکانی ~ و ^ ئاماژە بە دابەشکردنەکە دەکەن.

  3. How did we get there?چۆن گەیشتینە ئەوێ؟

    The next File line up: line 5 called average(0, 0). So count was 0: the real fix is in line 5 (or average should refuse a count of 0).

    دێڕی Fileـی دواتر بەرەو سەرەوە: دێڕی 5 average(0, 0)ـی بانگ کرد. کەواتە count 0 بوو: چاککردنی ڕاستەقینە لە دێڕی 5دایە (یان average دەبێت ژمارەی 0 ڕەت بکاتەوە).

Quick checkپشکنینی خێراIn a traceback, which line tells you the type of error?لە ، کام دێڕ جۆری هەڵەکەت پێ دەڵێت؟

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 ناتوانێت بیبینێت. کۆدەکە دروستە و تا کۆتایی کار دەکات، بەڵام ئەوە ناکات کە مەبەستت بوو. تاکە نیشانە ئەنجامێکی هەڵەیە، بۆیە دەبێت بزانیت ئەنجامی ڕاست چییە و بەراوردی بکەیت.

average3.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
a = 70
b = 80
c = 90
total = a + b
average = total / 3
print(average)

(The lines a = 70 store values in variables; they are explained in the next lesson.) Two more classic logic errors:

(دێڕەکانی وەک a = 70 لە هەڵدەگرن؛ لە وانەی داهاتوو ڕوون کراونەتەوە.) دوو کلاسیکیی تر:

order_of_operations.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
a = 70
b = 90
print(a + b / 2)
print((a + b) / 2)
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⁩). بڕوانە کارپێکەرەکان و دەربڕینەکان.

lost_newline.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Row 1", end="  ")
print
print("Row 2")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

print without () only names the function and does nothing, so no new line is printed and the rows run together. Write print(). (In old Python 2, print alone 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()⁩ ڕاهێنانی لەسەر دەکەیت.

Quick checkپشکنینی خێراWhy can't Python warn you about a logic error?بۆچی Python ناتوانێت ئاگادارت بکاتەوە لە ؟

Debugging habits that workخووە سوودبەخشەکانی کرمەکوژی

  1. Read the messageپەیامەکە بخوێنەوە

    Start with the last line of the traceback. Note the error type and the line number.

    لە دوایین دێڕی دەست پێ بکە. جۆری هەڵەکە و ژمارەی دێڕەکە بنووسە.

  2. Look at the line, and the one beforeسەیری دێڕەکە و دێڕی پێشی بکە

    Open that line. If it looks fine, check the end of the line above.

    ئەو دێڕە بکەرەوە. ئەگەر دروست دەردەکەوێت، کۆتایی دێڕی سەرەوەی بپشکنە.

  3. Make the problem smallکێشەکە بچووک بکەرەوە

    Run the program with the simplest input that still shows the bug, so you can predict the right answer.

    بە سادەترین جێبەجێ بکە کە هێشتا پیشان دەدات، بۆ ئەوەی بتوانیت وەڵامی ڕاست پێشبینی بکەیت.

  4. Print the valuesنرخەکان چاپ بکە

    Add a temporary print() that shows the variables you doubt, then remove it when you are done.

    print()ـێکی کاتی زیاد بکە کە ئەو پیشان بدات کە گومانت لێیانە، پاشان کاتێک تەواو بوویت لای ببە.

  5. Trace by handبە دەست بەدوای بچۆ

    Fill in a trace table, or let Thonny step through the lines.

    پڕ بکەرەوە، یان با Thonny هەنگاو بە هەنگاو بە دێڕەکاندا بڕوات.

  6. Change one thing, test againیەک شت بگۆڕە، دووبارە تاقی بکەرەوە

    Fix one thing at a time and run again. Changing many things at once hides which change helped.

    هەر جارە یەک شت چاک بکە و دووبارە جێبەجێی بکەرەوە. گۆڕینی چەند شتێک پێکەوە دەیشارێتەوە کام گۆڕانکاری یارمەتی دا.

  7. 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.

    کۆدەکەت دێڕ بە دێڕ بۆ هاوپۆلێک ڕوون بکەرەوە، تەنانەت بۆ ئیستیکانێک چا. وتنی هەر هەنگاوێک بە دەنگی بەرز زۆرجار هەڵەکە ئاشکرا دەکات.

Quick checkپشکنینی خێراYou changed four lines at once and now the program works. What is the problem with that?چوار دێڕت پێکەوە گۆڕی و ئێستا کار دەکات. کێشەی ئەمە چییە؟

Introduction to Programming and Computer Science - Full CoursefreeCodeCamp.org · Englishئینگلیزی · 11.7M viewsبینین · 249K likesلایک · Partبەشی 55:22–1:00:25 (ofلە 1:59:09)

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خووە سوودبەخشەکانی کرمەکوژی

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.

ڕێگەت پێ دەدات بە دەست جێبەجێ بکەیت. بۆ هەر ستوونێک و بۆ ستوونێک دروست بکە. دێڕ بە دێڕ بە بڕۆ و هەموو نوێ بنووسە. پاشان هەر بەراورد بکە لەگەڵ ئەوەی دەبوو بێت: یەکەم هەڵە دێڕە باگدارەکەت پیشان دەدات. بۆ هەر و لە هەر زمانێکدا بەکار دێن؛ وەشانە گشتییەکەی بەبێ کۆد لە شاشەی پشکنین بە خشتەی بەدواداچوون هەیە.

Animation: the six-line average program is traced. A highlight moves from line 1 to line 6 and a trace table fills one row per line: a = 70, b = 80, c = 90, total = 150, average = 50.0, output 50.0. In the last frame the total row is circled and marked: should be 240, line 4 forgot c.
Animation: the six-line average program is traced. A highlight moves from line 1 to line 6 and a trace table fills one row per line: a = 70, b = 80, c = 90, total = 150, average = 50.0, output 50.0. In the last frame the total row is circled and marked: should be 240, line 4 forgot c.
A trace table shows the first value that goes wrong. یەکەم پیشان دەدات کە هەڵە دەبێت.CêIT
Trace table of average3.py ⁦average3.py⁩
LineدێڕabctotalaverageOutput
170
27080
3708090
4708090150 (should be 240)⁦150⁩ (دەبوو ⁦240⁩ بێت)
570809015050.0
670809015050.050.0
Quick checkپشکنینی خێراIn a trace table, what does one row show?لە ، یەک ڕیز چی پیشان دەدات؟

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.

سلاید و پەڕەکانی تاقیگەی ڕەسەنی ئەم کۆرسە ڕاستەقینەیان تێدا بوو. هەر یەکێکیان لە وانەی خۆیدا چاک کراوە؛ لێرەدا لە تەنیشت یەکەوە دانراون، بۆ ئەوەی سێ جۆرەکە لە کۆدی ڕاستەقینەدا ببینیت.

Five bugs from the original material, each fixed in its lessonپێنج لە ماددە ڕەسەنەکەدا، هەر یەکێکیان لە وانەی خۆیدا چاک کراوە
BugKindجۆرWhat happensچی ڕوو دەداتFixed inلە کوێ چاک کراوە
A multiplication table ends each row with print instead of print()خشتەیەکی لێکدان هەر ڕیزێک بە print کۆتایی پێ دەهێنێت لە جیاتی print()LogicNo 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 بەبێ :SyntaxSyntaxError: 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ـێکدا وەردەگرێت؛ کاتێک بچووکترین ژمارە یەکەم بێت هەرگیز دروست نابێتRuntimeNameError: 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⁩ـەم بۆ زیاد کراLogicNo 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وانەی لیستەکان، ئاماژەکان، کۆپی و کردارەکان
Quick checkپشکنینی خێراWhich bug from the table gives no error message at all?کام لە خشتەکەدا هیچ نادات؟

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ڕاهێنان

ExerciseڕاهێنانWhich kind?کام جۆر؟

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وەڵامەکە پیشان بدە
ProgramKindجۆرPython 3.12 says⁦Python 3.12⁩ دەڵێت
(a)SyntaxSyntaxError: '(' was never closed
(b)RuntimeZeroDivisionError: division by zero
(c)Logicprints The sum of 2 and 3 is 6 (it multiplies)The sum of 2 and 3 is 6 چاپ دەکات (لێکدان دەکات)
(d)RuntimeNameError: name 'prnt' is not defined. Did you mean: 'print'?
(e)SyntaxIndentationError: unexpected indent
(f)RuntimeValueError: 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 بوونی نییە کە دێڕەکە جێبەجێ دەبێت.

ExerciseڕاهێنانRead the tracebackڕێچکەی هەڵە بخوێنەوە

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. (1) ZeroDivisionError: division by zero.
  2. (2) Line 2, inside the function average: return total / count.
  3. (3) Line 5: average(0, 0), so count was 0.
  4. (4) Do not ask for the average of zero marks: remove or change line 5. (Later you will also learn to check count before dividing.)
  1. ⁦(1)⁩ هەڵەکە: ZeroDivisionError: division by zero.
  2. ⁦(2)⁩ دێڕی 2، لەناو average: return total / count.
  3. ⁦(3)⁩ دێڕی 5: average(0, 0)، کەواتە count 0 بوو.
  4. ⁦(4)⁩ داوای تێکڕای سفر نمرە مەکە: دێڕی 5 لاببە یان بیگۆڕە. (دواتر فێر دەبیت پێش دابەشکردن count بپشکنیت.)
ExerciseڕاهێنانTrace and fixبەدوای بچۆ و چاکی بکە

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 چاپ دەکات. پڕ بکەرەوە و بدۆزەرەوە.

tea_bill.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
price = 2000
count = 3
total = price + count
print("Total:", total)
Show answerوەڵامەکە پیشان بدە
LineدێڕpricecounttotalOutput
12000
220003
3200032003 (should be 6000)⁦2003⁩ (دەبوو ⁦6000⁩ بێت)
4200032003Total: 2003

Line 3 adds instead of multiplying. Fixed:

دێڕی 3 کۆ دەکاتەوە لە جیاتی لێکدان. چاککراو:

tea_bill.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
price = 2000
count = 3
total = price * count
print("Total:", total)
ExerciseڕاهێنانOne of eachیەکێک لە هەر جۆرێک

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)⁩ و نمرەی دووەم چاپ بکات. یەک ، یەک و یەک تێدایە. یەک یەک چاکیان بکە و دوای هەر چاککردنێک جێبەجێی بکەرەوە.

marks.pyPYTHON
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وەڵامەکە پیشان بدە
  1. 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).

  2. Run 2: it prints Average: 115.0, then NameError: name 'mak2' is not defined. Did you mean: 'mark2'? on line 5. Write mark2.

    جێبەجێکردنی 2: Average: 115.0 چاپ دەکات، پاشان NameError: name 'mak2' is not defined. Did you mean: 'mark2'? لە دێڕی 5. بنووسە mark2.

  3. Run 3: no error, but Average: 115.0 is wrong (70 + 90 / 2). Add brackets: (mark1 + mark2) / 2.

    جێبەجێکردنی 3: هیچ هەڵەیەک نییە، بەڵام Average: 115.0 هەڵەیە ⁦(70 + 90 / 2)⁩. کەوانە زیاد بکە: (mark1 + mark2) / 2.

marks.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
mark1 = 70
mark2 = 90
average = (mark1 + mark2) / 2
print("Average:", average)
print("Second mark:", mark2)

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 …ـی سەرەوەی دەڵێت لە کوێ.
  • بە هێمنی بکە: بخوێنەوە، سەیر بکە، کێشەکە بچووک بکەرەوە، چاپ بکە، بە دەست بەدوای بچۆ، هەر جارە یەک شت بگۆڕە.
Review questionsپرسیارەکانی پێداچوونەوە
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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Variables and Data Typesگۆڕاوەکان و جۆرەکانی دراوە

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.