PythonPython · Chapterبەش 7 · Lectureوانە 7.1
The while Loopخولی while
Why programs need loops, the two kinds of loops, and Python's while loop: its condition, body and iterations, a three-step way to design a loop, counters, accumulators and sentinel values, infinite loops, and asking again until the input is valid.
بۆچی پێویستیان بە هەیە، دوو جۆرەکەی و : ، جەستە و ، ڕێگەیەکی سێ هەنگاوی بۆ داڕشتنی ، ، و ، و دووبارە پرسیارکردن تا دروست دەبێت.
What you will learnچی فێر دەبیت
- Explain what a loop is and choose between a condition-controlled and a count-controlled loop.
- Write and trace a
whileloop: condition, body, iterations and the value of the counter at the end. - Design a loop in three steps and use counters, accumulators and sentinel values.
- Recognise and stop infinite loops, and use
while Truewithbreakfor input validation.
- ڕوونی بکەیتەوە چییە و لە نێوان و یەکێکیان هەڵبژێریت.
- بنووسیت و بەدوایدا بچیت: ، جەستە، و نرخی لە کۆتاییدا.
- بە سێ هەنگاو دابڕێژیت و ، و بەکار بهێنیت.
- بناسیتەوە و ڕایانبگریت و
while Trueلەگەڵbreakبۆ بەکار بهێنیت.
Why do we need loops?بۆچی پێویستمان بە خول هەیە؟
So far, every line of a program ran once, from top to bottom. A loop tells the program to run statements again and again, and it controls how many times a group of operations is repeated. The course report puts it this way: a loop is a sequence of instructions that is repeated until a certain condition is reached.
تا ئێستا هەموو دێڕێکی یەکجار جێبەجێ دەبوو، لە سەرەوە بۆ خوارەوە. بە دەڵێت دووبارە و دووبارە جێبەجێ بکات و کۆنترۆڵی ئەوە دەکات کە کۆمەڵێک کار چەند جار دووبارە دەبێتەوە. ڕاپۆرتی کۆرسەکە بەم شێوەیە دەیڵێت: زنجیرەیەک کە دووبارە دەبێتەوە تا دەگاتە .
An example: you must show Programming is fun! 100 times. Without a loop you would type the same print line 100 times — tedious, easy to get wrong, and hard to change to 1,000 times.
نموونەیەک: دەبێت Programming is fun! 100 جار پیشان بدەیت. بەبێ دەبێت هەمان دێڕی print 100 جار بنووسیت — بێزارکەرە، هەڵەی تێ دەکەوێت و گۆڕینی بۆ 1000 جار قورسە.
print("Programming is fun!")
print("Programming is fun!")
# ... 100 times in total
print("Programming is fun!")count = 0
while count < 100:
print("Programming is fun!")
count = count + 1Programming is fun! Programming is fun! Programming is fun! … Programming is fun!
100 lines in total; the middle lines are left out here.
کۆی گشتی 100 دێڕە؛ دێڕەکانی ناوەڕاست لێرەدا نەنووسراون.
This chapter teaches loops step by step: the kinds of loops, the while loop and how to design a loop (this lesson), the for loop with range() (next lesson), the loop control statements break and continue (Controlling loops) and loops inside loops (Nested loops).
ئەم بەشە هەنگاو بە هەنگاو فێر دەکات: جۆرەکانی ، و چۆنیەتیی داڕشتنی (ئەم وانەیە)، لەگەڵ range() (وانەی داهاتوو)، کۆنترۆڵکردنی ، break و continue (کۆنترۆڵکردنی خولەکان) و لە ناو (خولە ناوەکییەکان).
Two kinds of loopsدوو جۆری خول
There are two main kinds of loops. A condition-controlled loop repeats as long as a condition is true; in Python this is the while loop. A count-controlled loop repeats a known number of times, or once for each item of a sequence; in Python this is the for loop.
دوو جۆری سەرەکیی هەیە. دووبارە دەبێتەوە تا ڕاست بێت؛ لە ئەمە . ژمارەیەکی زانراو جار دووبارە دەبێتەوە، یان بۆ هەر دانەیەکی جارێک؛ لە ئەمە .
| Loop | Checks the condition… دەپشکنێت… | Use it when…کەی بەکاری بهێنیت | In Pythonلە |
|---|---|---|---|
while | before every roundپێش هەموو | you do not know how many rounds: ‘until the user types 0’نازانیت چەند : «تا 0 دەنووسێت» | yesبەڵێ |
for | — (it walks through a sequence)— (بە دەڕوات) | you know the count or have a sequence: ‘10 times’, ‘each letter’ژمارەکە دەزانیت یان هەیە: «10 جار»، «هەر پیتێک» | yesبەڵێ |
do … while | after every round, so the body runs at least onceدوای هەموو ، بۆیە لانیکەم یەکجار جێبەجێ دەبێت | menus that must appear at least once کە دەبێت لانیکەم یەکجار دەربکەون | no — see belowنەخێر — خوارەوە ببینە |
A coach can say "Run 5 laps" — you know the count before you start, like a for loop. Or the coach can say "Keep running laps while I have not blown the whistle" — you check a condition before each lap, like a while loop.
ڕاهێنەر دەتوانێت بڵێت «5 خولانەوە ڕابکە» — پێش دەستپێکردن ژمارەکە دەزانیت، وەک . یان دەتوانێت بڵێت «تا فیکە لێ نەدەم بەردەوام بە لە ڕاکردن» — پێش هەر خولانەوەیەک دەپشکنیت، وەک .
Where the analogy stops workingسنووری ئەم نموونەیە
A coach can whistle in the middle of a lap; a while loop checks only at the start of each round, so a round that has started is always finished.
ڕاهێنەر دەتوانێت لە ناوەڕاستی خولانەوەیەکدا فیکە لێ بدات؛ تەنها لە سەرەتای هەر دەپشکنێت، بۆیە کە دەستی پێکردبێت هەمیشە تەواو دەکرێت.
The while loopخولی while
A loop that checks its condition first and repeats its block as long as the condition is true. If the condition is false at the start, the block never runs.
جۆرێکە لە کە سەرەتا دەپشکنێت و مادام ڕاست بێت، بلۆکەکەی دووبارە دەکاتەوە. ئەگەر مەرجەکە لە سەرەتاوە هەڵە بێت، بلۆکەکە هەرگیز جێبەجێ ناکرێت.
A while loop runs its statements again and again as long as a condition remains true. Its shape is:
دووبارە و دووبارە جێبەجێ دەکات تا ڕاست بمێنێتەوە. شێوەکەی ئەمەیە:
while loop-continuation-condition:
# loop body
statement(s)- The loop-continuation-condition is a Boolean expression: it is
TrueorFalse. - It is checked before every round. If it is
True, the loop body (the indented lines) runs once. - One run of the loop body is called an iteration (a repetition).
- When the condition is
False, the whole loop ends and the program goes on with the first statement after the loop.
- :
Trueـە یانFalse. - پێش هەموو دەپشکنرێت. ئەگەر
Trueبێت، (دێڕە بەرەوناوچووەکان) یەکجار جێبەجێ دەبێت. - یەکجار جێبەجێبوونی پێی دەوترێت .
- کاتێک
Falseبێت، هەموو کۆتایی دێت و بە یەکەم دوای بەردەوام دەبێت.
In Kurdish: how the while loop works in Python.
بە کوردی: لە چۆن کار دەکات.
Kurdish (Sorani), whole video (4:14).
ڤیدیۆیەکی کوردی (سۆرانی)، هەموو ڤیدیۆکە (4:14).
Example 1: a counter that stops at 100نموونەی 1: ژمێرەرێک کە لە 100دا دەوەستێت
Here is the 100-times program again, with its parts named. count is a counter: it starts at 0, the condition count < 100 is checked before every round, and the body prints one line and adds 1 to count. After 100 rounds count is 100, the condition is False and the loop ends.
ئەمە دیسان 100 جارییەکەیە، بە ناوی بەشەکانییەوە. count : لە 0ەوە دەست پێدەکات، count < 100 پێش هەموو دەپشکنرێت و دێڕێک چاپ دەکات و 1 دەخاتە سەر count. دوای 100 ، count دەبێتە 100، False دەبێت و کۆتایی دێت.
count = 0
while count < 100:
print("Programming is fun!")
count = count + 1Programming is fun! Programming is fun! Programming is fun! … Programming is fun!
100 lines in total; the middle lines are left out here.
کۆی گشتی 100 دێڕە؛ دێڕەکانی ناوەڕاست لێرەدا نەنووسراون.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Set the counter before the loop.
پێش دابنێ.
- Lineدێڕی 2
The loop-continuation-condition.
.
- Lineدێڕی 3-4
The loop body: both lines are indented, so both repeat.
: هەردوو دێڕەکە بەرەوناو چوون، بۆیە هەردووکیان دووبارە دەبنەوە.
Example 1: a counter that stops at 100نموونەی 1: ژمێرەرێک کە لە 100دا دەوەستێت
Example 1, step by stepنموونەی 1، هەنگاو بە هەنگاو
To watch every step, the animation runs a short version with count < 3. Follow the highlighted line, the value of count, the condition and the output.
بۆ ئەوەی هەموو هەنگاوێک ببینیت، ئەنیمەیشنەکە وەشانێکی کورت بە count < 3 جێبەجێ دەکات. شوێن دێڕە ڕەنگکراوەکە، نرخی count، و بکەوە.


count = 0
while count < 3:
print("Programming is fun!")
count = count + 1
print("Done, count is", count)Programming is fun! Programming is fun! Programming is fun! Done, count is 3
Counting from 1 to 10ژماردن لە 1 تا 10
The course report shows the same idea with numbers: print 1 to 10. This is its Python code as written in the report:
ڕاپۆرتی کۆرسەکە هەمان بیرۆکە بە ژمارە پیشان دەدات: 1 تا 10 چاپ بکە. ئەمە کۆدەکەیەتی بە ، وەک لە ڕاپۆرتەکەدا نووسراوە:
i=1
while (i <= 10):
print(i)
i += 11 2 3 4 5 6 7 8 9 10
It works. Three details: i += 1 is the short form of i = i + 1 (see augmented assignment); the brackets around the condition are allowed but not needed in Python; and PEP 8, Python's style guide, asks for 4 spaces of indentation instead of 2. Here is the same loop in that style, with one extra line after the loop:
کار دەکات. سێ وردەکاری: i += 1 شێوە کورتەکەی i = i + 1ـە (بڕوانە شێوە کورتەکانی وەک +=)؛ کەوانەکانی دەوری ڕێگەپێدراون بەڵام لە پێویست نین؛ PEP 8، ڕێنمایی شێوازی ، داوای 4 دەکات بۆ لەبری 2. ئەمە هەمان بەو شێوازە، لەگەڵ دێڕێکی زیادە دوای :
i = 1
while i <= 10:
print(i)
i += 1
print("After the loop, i is", i)1 2 3 4 5 6 7 8 9 10 After the loop, i is 11
i | i <= 10 | printedچاپکراو |
|---|---|---|
| 1 | True | 1 |
| 2 | True | 2 |
| 3 | True | 3 |
| … | … | … |
| 10 | True | 10 |
| 11 | False | — the loop ends— کۆتایی دێت |
Three steps to design a loopسێ هەنگاو بۆ داڕشتنی خولێک
Writing a loop that works correctly is not easy at first. The course gives three steps. We follow them for a small task: count down 5, 4, 3, 2, 1 and then print Kick-off!.
نووسینی کە بە دروستی کار بکات لە سەرەتادا ئاسان نییە. کۆرسەکە سێ هەنگاو دەدات. بۆ ئەرکێکی بچووک پەیڕەویان دەکەین: لە 5ەوە بەرەو خوار بژمێرە، 4، 3، 2، 1 و پاشان Kick-off! چاپ بکە.
- Find what repeatsئەوە بدۆزەرەوە کە دووبارە دەبێتەوە
Identify the statements that need to be repeated: print the number, then make it one smaller.
هەموو کە دەبێت دووبارە ببێتەوە دیاری بکە: ژمارەکە چاپ بکە، پاشان یەکێکی لێ کەم بکەرەوە.
step1.txtTEXTType in a terminalلە تێرمیناڵدا بینووسە print(seconds) seconds -= 1 - Wrap them in a loopبیانخەرە ناو خولێکەوە
Put those statements inside a loop that would run for ever:
while True:. We will add the condition in the next step.بخەرە ناو کە بۆ هەمیشە کار دەکات:
while True:. لە هەنگاوی دواتردا زیاد دەکەین.step2.txtTEXTType in a terminalلە تێرمیناڵدا بینووسە while True: print(seconds) seconds -= 1 - Write the condition and the control statementsمەرجەکە و ڕستەکانی کۆنترۆڵ بنووسە
Replace
Truewith the loop-continuation-condition (seconds > 0) and add the statements that control the loop: set the start value before the loop. The lineseconds -= 1already moves the loop towards its end.Trueبە (seconds > 0) بگۆڕە و کۆنترۆڵکردنی زیاد بکە: نرخی سەرەتا پێش دابنێ. دێڕیseconds -= 1پێشتر بەرەو کۆتایی دەبات.countdown.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە seconds = 5 while seconds > 0: print(seconds) seconds -= 1 print("Kick-off!")Outputدەرئەنجام5 4 3 2 1 Kick-off!
Counters and accumulatorsژمێرەرەکان و گۆڕاوەکانی کۆکردنەوە
Two patterns appear in almost every loop. A counter counts rounds or things: it starts at 0 and grows by 1 (count += 1). An accumulator collects a result: it starts at 0 (or "" for text) and every round adds to it (total += number). Both are set before the loop, so they are not reset in every round.
دوو شێواز لە زۆربەی دەردەکەون. یان شتەکان دەژمێرێت: لە 0ەوە دەست پێدەکات و هەر جارێک 1 زیاد دەکات (count += 1). ئەنجامێک کۆ دەکاتەوە: لە 0ەوە دەست پێدەکات (یان "" بۆ دەق) و هەموو شتێکی دەخاتە سەر (total += number). هەردووکیان پێش دادەنرێن، بۆ ئەوەی لە هەموو سفر نەکرێنەوە.
n = 100
total = 0
number = 1
while number <= n:
total += number
number += 1
print("The sum of 1 to", n, "is", total)The sum of 1 to 100 is 5050
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The accumulator starts at 0.
لە 0ەوە دەست پێدەکات.
- Lineدێڕی 3
numberis the counter: 1, 2, …, 100.number: 1، 2، …، 100.- Lineدێڕی 5
Add this round's number to the total.
ژمارەی ئەم بخەرە سەر کۆکە.
A quick check with the well-known formula for the sum of the whole numbers 1 to n: .
پشکنینێکی خێرا بە هاوکێشە ناسراوەکەی کۆی ژمارەکانی 1 تا n: .
Stopping at a special valueوەستان لە نرخێکی تایبەت
Often you do not know how many values the user will type. Then agree on a special stop value, a sentinel value, such as 0. The loop runs while the value is not the sentinel. Read the first value before the loop and the next one at the end of the body, so the test always sees the newest value.
زۆرجار نازانیت چەند نرخ دەنووسێت. ئەوکات لەسەر نرخێکی تایبەتی وەستان ڕێک بکەون، ، وەک 0. کار دەکات تا نرخەکە نەبێت. یەکەم نرخ پێش بخوێنەرەوە و نرخی دواتر لە کۆتایی ، بۆ ئەوەی پشکنینەکە هەمیشە نوێترین نرخ ببینێت.
total = 0
count = 0
price = int(input("Price (0 to stop): "))
while price != 0:
total += price
count += 1
price = int(input("Price (0 to stop): "))
print("Items:", count)
print("Total:", total, "dinars")Price (0 to stop): 1500 Price (0 to stop): 250 Price (0 to stop): 4000 Price (0 to stop): 0 Items: 3 Total: 5750 dinars
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The first value is read before the loop.
یەکەم نرخ پێش دەخوێنرێتەوە.
- Lineدێڕی 4
0 is the sentinel: it is never a real price.
0 : هەرگیز نرخێکی ڕاستەقینە نییە.
- Lineدێڕی 7
The next value is read at the end of the body.
نرخی دواتر لە کۆتایی دەخوێنرێتەوە.
Infinite loops and Ctrl+Cخولە بێکۆتاکان و Ctrl+C
If the condition never becomes False, the loop never ends: an infinite loop. The program seems frozen, or prints without stopping. Stop it with the keyboard shortcut Ctrl+C in the terminal (online editors usually have a Stop button). Python then stops with KeyboardInterrupt.
ئەگەر هەرگیز False نەبێت، هەرگیز کۆتایی نایەت: . وا دەردەکەوێت کە ڕاوەستاوە، یان بێ وەستان چاپ دەکات. بە Ctrl+C لە ڕایبگرە (دەستکاریکەرە ئۆنلاینەکان زۆرجار دوگمەی Stopـیان هەیە). ئەوکات بە KeyboardInterrupt دەوەستێت.
count = 0
while count < 3:
print("Programming is fun!")
count = count + 1Programming is fun!
Programming is fun!
Programming is fun!
… (it never stops, until you press Ctrl+C)
^CTraceback (most recent call last):
File "fun_bug.py", line 3, in <module>
print("Programming is fun!")
KeyboardInterruptReal run stopped with Ctrl+C (the terminal shows it as ^C).
جێبەجێکردنێکی ڕاستەقینە کە بە Ctrl+C ڕاگیراوە ( وەک ^C پیشانی دەدات).
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
Not indented, so not in the body:
countstays 0 andcount < 3is alwaysTrue.بەرەوناو نەچووە، بۆیە لە نییە:
countبە 0 دەمێنێتەوە وcount < 3هەمیشەTrueە.
A second cause: a condition that jumps over its target. Counting in steps of 3 never makes n exactly 10:
هۆکارێکی دووەم: کە بەسەر ئامانجەکەیدا باز دەدات. ژماردن بە هەنگاوی 3 هەرگیز n ڕێک ناکات بە 10:
n = 0
while n != 10:
print(n)
n += 30 3 6 9 12 15 18 21 … (for ever: n is never exactly 10)
The first 8 lines of a real run, then stopped.
یەکەم 8 دێڕی جێبەجێکردنێکی ڕاستەقینە، پاشان ڕاگیرا.
< لەبری != بەکار بهێنەskips_ten_fixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەn = 0
while n < 10:
print(n)
n += 30 3 6 9
How a for loop repeats code once for each item, so you can see what one iteration (one pass through the loop) is.
دەبینیت چۆن بۆ هەر یەک جار کۆدەکە جێبەجێ دەکات و چییە: یەک جار جێبەجێبوونی ناوەڕۆکی .
Plays 5:05-10:14, the while-loop chapter: counter, break, and an infinite while True loop.
لە 5:05 تا 10:14 پەخش دەبێت، بەشی : ، break و بە while True.
Asking again until the input is rightدووبارە پرسیارکردن تا نێوخستەکە دروست دەبێت
Sometimes the program must ask again and again until the user types something valid. A common pattern is while True: — a loop that would run for ever — with a break that leaves it as soon as the input is good. (break is explained fully in Controlling loops.)
هەندێک جار دەبێت دووبارە و دووبارە بپرسێت تا شتێکی دروست دەنووسێت. شێوازێکی باو while True:ـە — کە بۆ هەمیشە کار دەکات — لەگەڵ breakـێک کە هەر کاتێک باش بوو لێی دەردەچێت. (break بە تەواوی لە کۆنترۆڵکردنی خولەکان ڕوون کراوەتەوە.)
while Trueask_age.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەwhile True:
age = int(input("Your age (1-120): "))
if 1 <= age <= 120:
break
print("Please type a number from 1 to 120.")
print("Thank you. Your age is", age)Your age (1-120): 0 Please type a number from 1 to 120. Your age (1-120): 200 Please type a number from 1 to 120. Your age (1-120): 19 Thank you. Your age is 19
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
A chained comparison:
1 <= ageandage <= 120together.:
1 <= ageوage <= 120پێکەوە.- Lineدێڕی 4
Good input: leave the loop.
باش: لە دەربچە.
- Lineدێڕی 5
Bad input: say why, and the loop asks again.
خراپ: هۆکارەکە بڵێ و دووبارە دەپرسێت.
Asking again until the input is rightدووبارە پرسیارکردن تا نێوخستەکە دروست دەبێت
Python has no do … while loopPython خولی do...whileـی نییە
Languages such as Java, C and C# have a do … while loop: it runs the body first and checks the condition afterwards, so the body always runs at least once — handy for a menu that must be shown before the user chooses. Python has no do … while. You get the same effect with while True: and a check with break at the end of the body.
زمانەکانی وەک Java، C و C# هەیە: سەرەتا جێبەجێ دەکات و دواتر دەپشکنێت، بۆیە هەمیشە لانیکەم یەکجار جێبەجێ دەبێت — بۆ کە دەبێت پێش هەڵبژاردنی پیشان بدرێت بەسوودە. نییە. هەمان ئەنجام بە while True: و پشکنینێک بە break لە کۆتایی بەدەست دەهێنیت.
while True:
print("1) Say hello")
print("2) Quit")
choice = input("Choose: ")
if choice == "1":
print("Hello!")
if choice == "2":
break
print("Bye")1) Say hello 2) Quit Choose: 1 Hello! 1) Say hello 2) Quit Choose: 2 Bye
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-4
The body runs before any test: the menu is always shown once.
پێش هەر پشکنینێک جێبەجێ دەبێت: هەمیشە یەکجار پیشان دەدرێت.
- Lineدێڕی 7-8
The test is at the end of the body, as in do … while.
پشکنینەکە لە کۆتایی ، وەک لە do … while.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
while true: | NameError: true is not a Python nameNameError: true ناوێکی نییە | while True: |
while count < 100 | SyntaxError: the colon is missingSyntaxError: دوو خاڵەکە (:) نییە | while count < 100: |
| The update line is not indentedدێڕی نوێکردنەوە بەرەوناو نەچووە | Infinite loop: the counter never changes: هەرگیز ناگۆڕێت | Indent it into the bodyبیبە ناو |
| No update line at allهیچ دێڕێکی نوێکردنەوە نییە | Infinite loop | Change the loop variable in the body لە ناو بگۆڕە |
i = 1, while i < 10: | Prints 1 to 9: one too few (off-by-one)1 تا 9 چاپ دەکات: یەکێک کەمتر () | while i <= 10: |
while n != 10: + n += 3 | Jumps over 10: infinite loopبەسەر 10دا باز دەدات: | while n < 10: |
| Counter or total set inside the loop یان کۆ لە ناو دانراوە | It is reset every roundلە هەموو سفر دەکرێتەوە | Set it before the loopپێش دایبنێ |
Exercisesڕاهێنانەکان
Without running it: what does this program print? Make a trace table with x and the condition.
بەبێ جێبەجێکردن: ئەم چی چاپ دەکات؟ بە x و دروست بکە.
x = 1
while x < 50:
x = x * 3
print(x)Show answerوەڵامەکە پیشان بدە
x before | x < 50 | new x, printedxی نوێ، چاپکراو |
|---|---|---|
| 1 | True | 3 |
| 3 | True | 9 |
| 9 | True | 27 |
| 27 | True | 81 |
| 81 | False | — end— کۆتایی |
x = 1
while x < 50:
x = x * 3
print(x)3 9 27 81
81 is printed although it is not below 50: the test happens before the multiplication, when x is still 27.
81 چاپ دەکرێت هەرچەندە لە 50 کەمتر نییە: پشکنینەکە پێش لێکدانەکە ڕوودەدات، کاتێک x هێشتا 27ە.
Use a while loop to print 10 20 30 … 100 on one line.
بەکار بهێنە بۆ چاپکردنی 10 20 30 … 100 لە یەک دێڕدا.
Hintئاماژە
print(n, end=" ") keeps the output on one line.
print(n, end=" ") لە یەک دێڕدا دەهێڵێتەوە.
Show answerوەڵامەکە پیشان بدە
n = 10
while n <= 100:
print(n, end=" ")
n += 10
print()10 20 30 40 50 60 70 80 90 100
Ask for a whole number and print the sum of its digits (for 2026: ).
داوای بکە و کۆی چاپ بکە (بۆ 2026: ).
Hintئاماژە
number % 10 is the last digit; number //= 10 removes it.
number % 10 دوایین ؛ number //= 10 لای دەبات.
Show answerوەڵامەکە پیشان بدە
number = int(input("A whole number: "))
digit_sum = 0
while number > 0:
digit_sum += number % 10
number //= 10
print("Sum of the digits:", digit_sum)A whole number: 2026 Sum of the digits: 10
Each round takes the last digit (6, 2, 0, 2) and cuts it off. When nothing is left, number is 0 and the loop ends.
هەر دوایین وەردەگرێت (6، 2، 0، 2) و لای دەبات. کاتێک هیچ نەما، number دەبێتە 0 و کۆتایی دێت.
12321 reads the same from both ends (a palindrome number), 123421 does not. Ask for a whole number and say whether it is a palindrome — with arithmetic only (% 10 and // 10), not with strings.
12321 لە هەردوو سەرەوە وەک یەک دەخوێنرێتەوە (ژمارەیەکی پالیندرۆم)، بەڵام 123421 نا. داوای بکە و بڵێ ئایا پالیندرۆمە — تەنها بە ژمێرکاری (% 10 و // 10)، نەک بە .
Hintئاماژە
Build the reversed number in a second variable: reverse = reverse * 10 + number % 10, then cut the last digit off with number //= 10.
ژمارە پێچەوانەکراوەکە لە دووەمدا دروست بکە: reverse = reverse * 10 + number % 10، پاشان دوایین بە number //= 10 لاببە.
Show answerوەڵامەکە پیشان بدە
number = int(input("A whole number: "))
original = number
reverse = 0
while number > 0:
reverse = reverse * 10 + number % 10
number //= 10
if reverse == original:
print(original, "is a palindrome")
else:
print(original, "is not a palindrome:", reverse)A whole number: 123421 123421 is not a palindrome: 124321
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Keep a copy: the loop destroys
number.کۆپییەک هەڵبگرە:
numberتێک دەدات.- Lineدێڕی 5
Move the last digit of
numberto the end ofreverse.دوایین
numberبگوازەرەوە بۆ کۆتاییreverse.
number beforenumber پێش | number % 10 | reverse afterreverse دوای |
|---|---|---|
| 123421 | 1 | 1 |
| 12342 | 2 | 12 |
| 1234 | 4 | 124 |
| 123 | 3 | 1243 |
| 12 | 2 | 12432 |
| 1 | 1 | 124321 |
With 12321 the reversed number is 12321 again, so the program prints 12321 is a palindrome. The string version of this test is in Working with strings.
بە 12321 ژمارە پێچەوانەکراوەکە دیسان 12321ە، بۆیە 12321 is a palindrome چاپ دەکات. هەمان پشکنین بە لە کارکردن لەگەڵ زنجیرەنووسەکاندایە.
Ask for a whole number above 0 and print all its factors on one line: the numbers from 1 to the number itself that divide it with no remainder (36 → 1 2 3 4 6 9 12 18 36).
داوای لە 0 گەورەتر بکە و هەموو دابەشکەرەکانی لە یەک دێڕدا چاپ بکە: ئەو ژمارانەی لە 1ەوە تا خودی ژمارەکە کە بەبێ دابەشی دەکەن (36 ← 1 2 3 4 6 9 12 18 36).
Hintئاماژە
Try every candidate d from 1 upwards; number % d == 0 means d divides number.
هەموو ژمارەیەکی d لە 1ەوە بەرەو سەر تاقی بکەرەوە؛ number % d == 0 واتە d ژمارەکە بەبێ دابەش دەکات.
Show answerوەڵامەکە پیشان بدە
number = int(input("A whole number: "))
d = 1
while d <= number:
if number % d == 0:
print(d, end=" ")
d += 1
print()A whole number: 36 1 2 3 4 6 9 12 18 36
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
dis the counter: 1, 2, 3, … up tonumber.d: 1، 2، 3، … تاnumber.- Lineدێڕی 4-5
Print
donly when it dividesnumberexactly.dتەنها کاتێک چاپ بکە کەnumberڕێک دابەش بکات.
A prime number such as 7 prints only 1 7. With for d in range(1, number + 1): the same loop is one line shorter — see The for loop and range().
ژمارەیەکی سەرەتایی وەک 7 تەنها 1 7 چاپ دەکات. بە for d in range(1, number + 1): هەمان یەک دێڕ کورتتر دەبێت — بڕوانە وانەی داهاتوو.
Change Example 1 so that count goes from 1 to 100 and every line starts with its number: 1 Programming is fun! … 100 Programming is fun!.
نموونەی 1 بگۆڕە بۆ ئەوەی count لە 1ەوە بڕوات تا 100 و هەر دێڕێک بە ژمارەکەی خۆی دەست پێ بکات: 1 Programming is fun! … 100 Programming is fun!.
Show answerوەڵامەکە پیشان بدە
count = 1
while count <= 100:
print(count, "Programming is fun!")
count = count + 11 Programming is fun! 2 Programming is fun! … 100 Programming is fun!
100 lines; the middle is left out.
100 دێڕ؛ ناوەڕاستەکەی نەنووسراوە.
Two changes: the start value (1 instead of 0) and the test (<= 100 instead of < 100). Changing only one of them gives 99 or 101 lines.
دوو گۆڕانکاری: نرخی سەرەتا (1 لەبری 0) و پشکنینەکە (<= 100 لەبری < 100). گۆڕینی تەنها یەکێکیان 99 یان 101 دێڕ دەدات.
Rewrite the report's loop so that it prints 10, 9, …, 1 on one line.
ڕاپۆرتەکە بنووسەرەوە بۆ ئەوەی 10، 9، …، 1 لە یەک دێڕدا چاپ بکات.
Show answerوەڵامەکە پیشان بدە
i = 10
while i >= 1:
print(i, end=" ")
i -= 1
print()10 9 8 7 6 5 4 3 2 1
Summaryپوختە
- A loop repeats statements.
whileis condition-controlled;foris count-controlled. while condition:checks the condition before every iteration and stops when it isFalse. The whole body is indented.- Design in three steps: find what repeats, wrap it in
while True:, then write the condition and the control statements. - Counters and accumulators start before the loop; a sentinel value can end input.
- If the condition never becomes
Falseyou get an infinite loop: stop it with Ctrl+C. while True:withbreakasks again until the input is valid, and replaces do … while.
- دووبارە دەکاتەوە.
while؛for. while condition:پێش هەموو دەپشکنێت و کاتێکFalseبوو دەوەستێت. هەموو بەرەوناو دەچێت.- بە سێ هەنگاو دایبڕێژە: ئەوەی دووبارە دەبێتەوە بیدۆزەرەوە، بیخەرە ناو
while True:، پاشان و کۆنترۆڵ بنووسە. - و پێش دەست پێدەکەن؛ دەتوانێت کۆتایی پێ بهێنێت.
- ئەگەر هەرگیز
Falseنەبێت دروست دەبێت: بە Ctrl+C ڕایبگرە. while True:لەگەڵbreakدووبارە دەپرسێت تا دروست دەبێت و جێگەی do … while دەگرێتەوە.
What is an iteration?خولانەوە چییە؟
One run of the loop body.
یەکجار جێبەجێبوونی .
A while loop's condition is False at the very start. How many times does the body run?مەرجی خولی while لە سەرەتاوە Falseە. جەستەی خولەکە چەند جار جێبەجێ دەبێت؟
Zero times.
سفر جار.
Why is while true: wrong?بۆچی while true: هەڵەیە؟
Python's constant is True; true gives a NameError.
نرخە جێگیرەکەی Trueـە؛ true NameError دەدات.
How do you get the effect of do … while in Python?چۆن کاریگەریی do … while لە Pythonـدا بەدەست دەهێنیت؟
while True: with an if … : break at the end of the body.
while True: لەگەڵ if … : break لە کۆتایی .
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
A while loop repeats while its condition stays True: asking again until the input is right, an infinite loop, and stopping at a special value.
دووبارە دەبێتەوە هەتا True بمێنێت: دووبارە پرسیارکردنەوە تا دروست بێت، و وەستان بە .
Whole video (6:58). Chapters: 0:50 example 1 (ask again for a name), 1:50 infinite loop, 2:25-6:35 three more examples.
هەموو ڤیدیۆکە (6:58). بەشەکان: 0:50 نموونەی 1 (دووبارە داوای ناو دەکاتەوە)، 1:50 ، 2:25 تا 6:35 سێ نموونەی تر.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
The for Loop and range()خولی for و range()
Related lecturesوانە پەیوەندیدارەکان
- Algorithms, Pseudocode and Flowchartsئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت
- The for Loop and range()خولی for و
range() - Controlling Loops: break and continueکۆنترۆڵکردنی خولەکان:
breakوcontinue - Nested Loops and Patternsخولە ناوەکییەکان و کێشانی شێوە
- Loops: while, do...while, for, foreach, break and continueخولەکان: while و do...while و for و foreach و break و continue
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python tutorial 3.2: First steps towards programming (the while loop)
- Python tutorial 4: More control flow tools
- Harvard CS50's Introduction to Programming with Python, notes 2: Loops (while loops, validating input)
- PEP 8: Style guide for Python code (indentation)
- Liang, Introduction to Programming Using Python — chapter list (chapter 5: Loops)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.