Counting loops · ספירות במחזוריות
Repeat with for
- A
forloop repeats code a fixed number of times. - The body of the loop is indented, like an
if. - A loop variable holds a new value on each pass.
חזרה עם for
- לולאת
forחוזרת על קוד מספר קבוע של פעמים. - גוף הלולאה מוזג, כמו ב-
if. - משתנה לולאה מחזיק ערך חדש בכל מעבר.
Counting with range
range(5)gives the numbers0, 1, 2, 3, 4(stops before 5).range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)counts in steps of 2.
ספירה עם range
range(5)נותן את המספרים0, 1, 2, 3, 4(עוצר לפני 5).range(1, 6)נותן1, 2, 3, 4, 5(התחלה כלולה, עצירה לא כלולה).range(0, 10, 2)סופר בצעדים של 2.
for i in range(5):
print(i)
Build a total (accumulator)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it on each pass to build a running total.
בניית סכום כולל (צובר)
- מצטבר הוא משתנה הגדל בתוך לולאה.
- התחיל אותו ב-
0לפני הלולאה. - הוסף אליו בכל עבר כדי לבנות סכום רציף.
total = 0
for n in range(1, 6):
total = total + n
print(total)
In Cambridge pseudocode
- A Python
for … rangeloop is the exam's count-controlledFOR … NEXT.
בפסאודוקוד קמברידג'
- לולאת Python
for … rangeהיא לולאתFOR … NEXTמבוקרת ספירה במבחן.
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Words and a number on one line
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)prints7 x 1 = 7.+cannot join a number to text. Use the comma, or turn the number into text withstr.
מילים ומספר בשורה אחת
- פסיק בתוך
printמוסיף רווח בין החלקים. - ה-
print("7 x", 1, "=", 7 * 1)מדפס7 x 1 = 7. +אינו יכול לחבר מספר לטקסט. השתמש בפסיק, או המר את המספר לטקסט באמצעותstr.
Common mistakes
range(n)counts0ton - 1, not1ton.range(a, b)stops beforeb.- The loop body must be indented under the
forline.
טעויות נפוצות
range(n)סופר מ0עדn - 1, ולא מ1עדn.range(a, b)נעצר לפניb.- גוף הלולאה חייב להיות מוזז לתוך (מוזז) תחת שורת ה
for.
Now you try
- Use a
forloop withrange(...)in each task. - Press Check answer to test your code.
כעת תנסו בעצמכם
- השתמש בלולאת
forעםrange(...)בכל משימה. - לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
A counting loop, pass by pass · לולאת ספירה, צעד אחר צעד
A for loop repeats its body; watch the counter and the running total. · לולאת for חוזרת על הגוף שלה; השקב את הסופר והסכום הנצבר.
Use a for loop to print the numbers 1 to 5, each on its own line. · השתמשו בלולאת for כדי להדפיס את המספרים 1 עד 5, כל אחד בשורה משלו.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Use a loop and an accumulator to add up every whole number from 1 to 100. Store the total in total. · השתמש בלולאה ובאקומולטור כדי לחבר כל המספרים שלמים מ-1 עד 100. אחסן את הסכום ב-total.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Multiply together every whole number from 1 to 5 (that is 1×2×3×4×5). Start product at 1 and store the result there. · כפול יחד כל המספרים שלמים מ-1 עד 5 (כלומר 1×2×3×4×5). התחל את product ב-1 ואחסן בה תוצאה.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Print exactly one line: 7 x 1 = 7. Finish the print that is already started. A comma between the parts prints them with spaces: print("7 x", 1, "=", 7 * 1). · הדפס בדיוק שורה אחת: 7 x 1 = 7. סיים את ה-print שהתחיל כבר. פסיק בין החלקים מדפיס אותם עם רווחים: print("7 x", 1, "=", 7 * 1).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Use a for loop to print the first five lines of the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 5 = 35. The print from the last task is already inside the loop — change the 1 into the loop variable. · השתמש בלולאת for להדפסת חמיש השורות הראשונות של טבלת הכפלה ב-7: 7 x 1 = 7, 7 x 2 = 14, עד ל-7 x 5 = 35. ה-print מהמשימה האחרונה נמצא כבר בתוך הלולאה — שינה את ה-1 למשתנה הלולאה.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.