Nested loops & 2-D lists · לולאות מקוננות ורשימות ממומד 2
A loop inside a loop
- You can put one loop inside another loop.
- For every pass of the outer loop, the inner loop runs fully.
- So an outer of 2 and an inner of 3 runs the body 2 × 3 = 6 times.
לולאה בתוך לולאה
- ניתן להכניס לולאה אחת בתוך לולאה אחרת.
- עבור כל עבר של הלולאה החיצונית, הלולאה הפנימית מופעלת ללא הפסקה.
- כך ש外层 של 2 ו层 פנימי של 3 מריצים את הגוף 2 × 3 = 6 פעמים.
for row in range(2):
for col in range(3):
print(row, col)
A list of lists
- A 2-D list is a list whose items are themselves lists.
- It models a grid or table: rows of columns.
- Reach one cell with two indexes:
grid[row][col].
רשימה של רשימות
- רשימה ד-2 היא רשימה whose פריטים הם בעצם רשימות.
- היא מדגמת גריד או טבלה: שורות ועמודות.
- לגשת לתא אחד משתי אינדקסים:
grid[row][col].
grid = [[1, 2], [3, 4]]
print(grid[0][1])
print(grid[1][0])
Loop over a grid
- Use a nested loop to visit every cell.
- The outer loop walks the rows; the inner loop walks the columns.
for row in grid:gives one row (a list) at a time.
לולאה על גריד
- השתמש בלולאה מקוננת כדי לבקר בכל תא.
- הלולאה החיצונית עוברת את ה-שורות; הלולאה הפנימית עוברת את ה-עמודות.
for row in grid:מספק שורה אחת (רשימה) בכל פעם.
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
In Cambridge pseudocode
- A 2-D list is the exam's 2-D array, written
ARRAY[1:rows, 1:cols].
בפסאודוקוד קמברידג'
- רשימה ד-2 היא מערך ד-2 של המבחן, כתוב כ
ARRAY[1:rows, 1:cols].
DECLARE grid : ARRAY[1:2, 1:3] OF INTEGER
FOR r ← 1 TO 2
FOR c ← 1 TO 3
OUTPUT grid[r, c]
NEXT c
NEXT r
Common mistakes
- The inner loop finishes all its turns for each single turn of the outer loop.
- Index a grid row first, then column:
grid[row][col].
טעויות נפוצות
- הלולאה הפנימית מסתיימת את כל העוברים שלה לכל עבר בודד של הלולאה החיצונית.
- אינדקס שורת גריד קודם, ואז עמודה:
grid[row][col].
Now you try
- Use a nested loop in each task.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- השתמש בלולאה מקוננת בכל משימה.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
A 2-D list is a grid · רשימה ממומד 2 היא טבלא
A 2-D list is rows of columns; reach a cell with grid[row][col]. · רשימה ממומד 2 היא שורות של עמודות; הגיע לתא עם grid[row][col].
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters. · השתמש בלולאה מצטבת להדפסת ריבוע כוכבים בגודל 3x3: שלושה שורות, כל אחד עם שלושה * תווים.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21). · סכם כל המספרים ברשימה ממומד 2 grid באמצעות לולאה מקוננת. אחסן את הסכום בtotal (התשובה היא 21).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Count how many cells in the 2-D list grid are equal to 0. Store the count in count (the answer is 3). · ספור כמה תאים ברשימת grid בתנאי 2 שווים ל0. שמור את הספירה בcount (התשובה היא 3).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.