Making decisions · קבלת החלטות
Making a choice
ifruns some code only when a condition is true.- The condition is followed by a colon
:. - The lines that belong to the
ifare indented (4 spaces).
ביצוע בחירה
ifמפעיל קוד רק כאשר תנאי מתקיים.- לאחר התנאי נמצא נקודה ופסקה
:. - השורות השייכות ל
ifמוזזות (4 רווחים).
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
השוואות
- תנאי הוא בדרך כלל השוואה שהתוצאה שלה היא נכון או שגוי.
==שווה,!=לא שווה,<><=>=.- שים לב:
==בודק שוויון; סימן יחיד=שומר ערך.
temperature = 30
if temperature > 25:
print("It is hot.")
if / elif / else
elif("else if") checks another condition if the first was false.elseruns when none of the conditions were true.- Python checks them in order and stops at the first true one.
if / elif / else
elif("אלף אם") בודק תנאי נוסף אם הראשון היה שגוי.elseמפעיל כשהם אין תנאי שהיה אמת.- פייתון בודק אותם בסדר ועוצר בראשון הנכון.
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
The remainder
a % bis what is left over whenais divided byb.year % 4 == 0means the year divides by 4 exactly — nothing left over.8 % 3is2, because 8 is two 3s with 2 left over.
שארית
a % bהוא מה שנשאר כאשרaמחולק ב-b.year % 4 == 0פירונו שהשנה מתחלקת ב-4 בדיוק — אין שארית.8 % 3שווה ל-2, כי 8 היא שתי פעמים 3 עם שארית של 2.
Combine conditions
andis true only when both sides are true.oris true when at least one side is true.notflips true to false and false to true.
שילוב תנאים
andנכון רק כאשר שני הצדדים נכונים.orנכון כאשר לפחות אחד מהצדדים נכון.notהופך נכון לשגוי ושגוי לנכון.
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
בפסאודוקוד קמברידג'
- Python
if/elif/elseתואם את ה-IF … ELSE … ENDIFו-CASEבמבחן.
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
CASE OF Grade
"A": OUTPUT "Top"
"B": OUTPUT "Good"
OTHERWISE OUTPUT "Keep trying"
ENDCASE
Common mistakes
- Compare with
==; a single=assigns. - Write
elif, notelse if, and end eachif/elif/elseline with:. - The indented block below
ifis what runs when the condition is true.
טעויות נפוצות
- השוואה ל-
==; סימן יחיד=מבצע הקצה. - כתוב
elif, לאelse if, וגמור כל שורה /if/elif/elseב-:. - המבנה הממוזג למטה ב-
ifהוא מה שמריץ כשהתנאי נכון.
Now you try
- When you Run, type the input in the box under the code and press Enter.
- The checks type the input for you. Read it, then decide what to print.
- Match the output text exactly.
כעת תנסו בעצמכם
- כאשר הפעל, הקלד את הקלט בתיבה מתחת לקוד ולחץ על Enter.
- הבדיקות מקלידות עבורך את הקלט. קרא אותו, ואז החלט מה להדפיס.
- התאם את טקסט הפלט בדיוק.
if / elif / else
Conditions are checked top-down; only the first true branch runs. · התנאים נבדקים למעלה למטה; רק הענף האמתי הראשון יוצא לפועל.
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass. · קרא ציון עם int(input()). הדפס Pass אם הוא 50 או יותר, אחרת הדפס Fail. עבור קלט 72, הדפס Pass.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Read a score. Print A if it is 80+, B if it is 60+, otherwise C. Use if / elif / else. For input 75, print B. · קרא ציון. הדפס A אם הוא 80+, B אם הוא 60+, אחרת C. השתמש ב-if / elif / else. עבור קלט 75, הדפס B.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Read an age. Print Yes if the age is from 13 to 19 (a teenager), otherwise No. Use and. For input 16, print Yes. · קרא גיל. הדפס Yes אם הגיל נע בין 13 ל-19 (קשיש), אחרת No. השתמש ב-and. עבור קלט 16, הדפס Yes.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Read a year with int(input()). A year divides by 4 when the remainder is 0: year % 4 == 0. Print Leap if it divides by 4, otherwise Common. For the input 2024, print Leap. · קרא שנה עם int(input()). שנה מתחלקת ב-4 כאשר שארית החלוקה היא 0: year % 4 == 0. הדפס Leap אם היא מתחלקת ב-4, אחרת הדפס Common. עבור הקלט 2024, הדפס Leap.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Dividing by 4 is not the whole rule. A year that also divides by 100 is not a leap year, unless it divides by 400 too. 1900 is the trap: it divides by 4 and by 100, so print Common. Print Leap or Common. · חלוקה ב-4 אינה הכלל המלא. שנה שמחלקת גם ב-100 היא לא שנה מעוברת, אלא אם כן היא מתחלקת גם ב-400. 1900 הוא המכשול: היא מתחלקת ב-4 וב-100, ולכן הדפס Common. הדפס Leap או Common.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.