מפגש 6: while, for, range, break ו-continue, ולולאת תפריט.Session 6: while, for, range, break and continue, and a menu loop.
while, for, range, break ו-continue, ולולאת תפריט. בסוף המפגש מוסיפים צעד לפרויקט המלווה.while, for, range, break and continue, and a menu loop. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
whilewhile
forfor
rangerange
צובריםAccumulators
break/continuebreak/continue
תפריטMenu loop
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
whilewhile
while: כל עודwhile: as long as
חוזר כל עוד התנאי TrueRepeats while the condition is True
משהו בתוך הלולאה חייב לשנות את התנאיSomething inside must change the condition
אחרת: לולאה אינסופיתOtherwise: an infinite loop
לולאתwhile מריצה את הבלוק שוב ושוב כל עוד התנאי נכון. בכל סיבוב התנאי נבדק מחדש. חובה שמשהו בתוך הלולאה יקרב אותה לסוף.A whileloop runs its block again and again as long as the condition is true. The condition is rechecked on every iteration. Something inside must move the loop toward its end.
אנלוגיהAnalogy
while הוא כמו לאכול מרק "כל עוד יש בצלחת": כל כף מקרבת לסוף. אם הכף הייתה ריקה, היינו אוכלים לנצח.while is like eating soup "as long as there's some in the bowl": each spoonful gets you closer to the end. With an empty spoon you'd eat forever.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
forfor
for: על כל פריטfor: for each item
for x in רצף:for x in sequence:
עובד על מחרוזות, רשימות ו-rangeWorks on strings, lists and range
המשתנה מקבל פריט חדש בכל סיבובThe variable gets a new item each pass
for עוברת על הפריטים של רצף, אחד אחרי השני. המשתנה שאחרי for מקבל בכל סיבוב את הפריט הבא. כשהפריטים נגמרים, הלולאה נגמרת לבד: אין סכנה ללולאה אינסופית.for walks through a sequence's items one by one. The variable after for gets the next item each pass. When the items run out the loop ends by itself: no risk of an infinite loop.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
python
for letter in"Dune":print(letter)
for book in ["Dune", "Emma", "Ulysses"]:print(f"📚 {book}")
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה.Give the class half a minute to guess, then move on.
forfor
for_demo.py
for letter in"Dune":print(letter)
for book in ["Dune", "Emma", "Ulysses"]:print(f"📚 {book}")
פלטOutput
D
u
n
e
📚 Dune
📚 Emma
📚 Ulysses
מריצים ומשווים לניחושים.Run it and compare with the guesses.
אנלוגיהAnalogy
for הוא כמו מורה שעובר בין השולחנות ובודק כל מחברת בתורה. הוא לא צריך לספור: כשהשולחנות נגמרים, הוא סיים.for is like a teacher walking past every desk to check each notebook in turn. No counting needed: when the desks run out, it's done.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
for_demo.py
for letter "Dune":print(letter)
מה Python עונהWhat Python says
SyntaxError: invalid syntax
SyntaxError: התבנית היא for <משתנה> in <רצף>:.SyntaxError: the pattern is for <variable> in <sequence>:.
שוכחים את in.Forgetting in.
נושאTopic 3
rangerange
range: לספורrange: counting
range(5) → 0..4range(5) → 0..4
range(1, 6) → 1..5range(1, 6) → 1..5
range(10, 0, -2) סופר לאחורrange(10, 0, -2) counts down
range יוצר רצף מספרים בלי לשמור את כולם בזיכרון. כמו בחיתוך: ההתחלה נכללת, הסוף לא. צעד שלישי קובע בכמה לקפוץ, וגם שלילי מותר.range makes a sequence of numbers without storing them all. Like slicing: the start is included, the end isn't. A third step sets how far to jump, and it may be negative.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
python
for i inrange(3):print(i, end=" ")
print()
for day inrange(1, 6):print(day, end=" ")
print()
for n inrange(10, 0, -3):print(n, end=" ")
print()
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה.Give the class half a minute to guess, then move on.
rangerange
range_demo.py
for i inrange(3):print(i, end=" ")
print()
for day inrange(1, 6):print(day, end=" ")
print()
for n inrange(10, 0, -3):print(n, end=" ")
print()
פלטOutput
0 1 2
1 2 3 4 5
10 7 4 1
מריצים ומשווים לניחושים.Run it and compare with the guesses.
אנלוגיהAnalogy
range הוא כמו מונה צעדים שמכוון מראש: "מ-1, עד לפני 6, בקפיצות של 1".range is like a pre-set step counter: "from 1, up to (not incl.) 6, in steps of 1".
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
range_demo.py
for i inrange(2.5):print(i)
מה Python עונהWhat Python says
TypeError: 'float' object cannot be interpreted as an integer
TypeError: 'float' object cannot be interpreted as an integer: range עובד רק עם int.TypeError: 'float' object cannot be interpreted as an integer: range takes int only.
מעבירים ל-range מספר עשרוני.Passing a decimal to range.
נושאTopic 4
צובריםAccumulators
צוברים: סכום, מונה, מקסימוםAccumulators: sum, count, max
מתחילים ב-0 לפני הלולאהStart at 0 before the loop
מוסיפים בכל סיבובAdd on every pass
משתמשים בתוצאה אחרי הלולאהUse the result after the loop
צובר הוא משתנה שמתחיל לפני הלולאה (בדרך כלל 0) ומשתנה בכל סיבוב. ככה סוכמים, סופרים, או מוצאים את הגדול ביותר.An accumulator is a variable set up before the loop (usually 0) that changes on every pass. That's how you sum, count, or find the largest.
אנלוגיהAnalogy
צובר הוא כמו קופה רושמת: מתחילה באפס בבוקר, וכל קנייה מוסיפה. בערב מסתכלים על הסכום.An accumulator is like a till: it starts at zero in the morning and every sale adds to it. In the evening you read the total.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
break/continuebreak/continue
break ו-continuebreak and continue
break יוצא מהלולאה מידbreak leaves the loop at once
continue מדלג לסיבוב הבאcontinue skips to the next pass
while True + break לתפריטיםwhile True + break for menus
break עוצר את הלולאה ויוצא ממנה. continue מדלג על שאר הסיבוב הנוכחי ועובר לבא. הצירוף while True עם break שימושי ללולאה שרצה עד שהמשתמש מבקש לצאת.break stops the loop and leaves it. continue skips the rest of the current pass and moves to the next. while True with break is handy for a loop that runs until the user asks to quit.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
python
for p in [96, 0, 412, -5, 280]:if p <0:print("נתון שגוי, עוצרים")
breakif p ==0:continueprint(p)
for p in [96, 0, 412, -5, 280]:if p <0:print("bad data, stopping")
breakif p ==0:continueprint(p)
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה.Give the class half a minute to guess, then move on.
break/continuebreak/continue
break_demo.py
for p in [96, 0, 412, -5, 280]:if p <0:print("נתון שגוי, עוצרים")
breakif p ==0:continueprint(p)
for p in [96, 0, 412, -5, 280]:if p <0:print("bad data, stopping")
breakif p ==0:continueprint(p)
פלטOutput
96
412
נתון שגוי, עוצרים
96
412
bad data, stopping
מריצים ומשווים לניחושים.Run it and compare with the guesses.
אנלוגיהAnalogy
continue הוא כמו לדלג על שיר בפלייליסט. break הוא לכבות את הנגן.continue is skipping a song in a playlist. break is switching the player off.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
break_demo.py
x =5if x >3:break
מה Python עונהWhat Python says
SyntaxError: 'break' outside loop
SyntaxError: 'break' outside loop: break שייך רק לתוך for או while.SyntaxError: 'break' outside loop: break only belongs inside a for or while.
משתמשים ב-break מחוץ ללולאה.Using break outside a loop.
נושאTopic 6
תפריטMenu loop
לולאת תפריטA menu loop
while True: מציג תפריטwhile True: shows a menu
כל בחירה = if/elifEach choice = if/elif
בחירת יציאה = breakThe quit choice = break
תוכניות טרמינל רבות בנויות כך: מציגים תפריט, קוראים בחירה, מבצעים, וחוזרים. כך יבנה גם הפרויקט המלווה שלנו.Many terminal programs work like this: show a menu, read a choice, act, repeat. Our companion project will work this way too.
אנלוגיהAnalogy
תפריט בלולאה הוא כמו מלצר שחוזר לשולחן שוב ושוב עד שמבקשים חשבון.A menu loop is like a waiter who keeps coming back to the table until you ask for the bill.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
חוזר כל עוד התנאי TrueRepeats while the condition is True
while: כל עודwhile: as long as
משהו בתוך הלולאה חייב לשנות את התנאיSomething inside must change the condition
while: כל עודwhile: as long as
אחרת: לולאה אינסופיתOtherwise: an infinite loop
while: כל עודwhile: as long as
for x in רצף:for x in sequence:
for: על כל פריטfor: for each item
עובד על מחרוזות, רשימות ו-rangeWorks on strings, lists and range
for: על כל פריטfor: for each item
המשתנה מקבל פריט חדש בכל סיבובThe variable gets a new item each pass
for: על כל פריטfor: for each item
range(5) → 0..4range(5) → 0..4
range: לספורrange: counting
range(1, 6) → 1..5range(1, 6) → 1..5
range: לספורrange: counting
range(10, 0, -2) סופר לאחורrange(10, 0, -2) counts down
range: לספורrange: counting
מה למדנוWhat we learned
חוזר כל עוד התנאי TrueRepeats while the condition is True
for x in רצף:for x in sequence:
range(5) → 0..4range(5) → 0..4
מתחילים ב-0 לפני הלולאהStart at 0 before the loop
break יוצא מהלולאה מידbreak leaves the loop at once
while True: מציג תפריטwhile True: shows a menu
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.
עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked