נבנה על ידיBuilt by Ariel Yohimovich

PythonPython

מבחן 2: לולאות ופונקציותExam 2: Loops and functions

for, while, range, פונקציות, החזרת ערכים ו-scope

for, while, range, functions, return values and scope

60 דקותminutes4 שאלות בחירהmultiple choice3 שאלות קודcoding questions100 נקודותpoints
60:00השעון רק עוזר להתאמן. אפשר גם בלי.The timer is just for practice. You can skip it.

חלק א: שאלות בחירה (5 נקודות לשאלה)Part A: multiple choice (5 points each)

התשובות מתגלות רק כשמסיימים את המבחן.Answers are revealed only when you finish the exam.

1. כמה פעמים תרוץ הלולאה?How many times does the loop run? 5
python
for i in range(2, 10, 2):
    print(i)
2. מה יודפס?What is printed? 5
python
def double(x):
    x * 2

print(double(3))
3. מה הערך של total בסוף?What's total at the end? 5
python
total = 0
for n in [1, 2, 3, 4, 5]:
    if n == 4:
        break
    total += n
4. מה יודפס?What is printed? 5
python
x = 5
def f():
    x = 1
    return x
print(f(), x)

חלק ב: שאלות קודPart B: coding questions

שאלהQuestion 5

ספרותDigits

כתבו digit_sum(n) שמחזירה את סכום הספרות של מספר שלם אי-שלילי, בלולאת while עם % ו-// (בלי להמיר למחרוזת).

Write digit_sum(n) returning the sum of a non-negative integer's digits, with a while loop using % and // (without converting to a string).

איך הפלט אמור להיראות?What should the output look like?
10 0
main.py
רמזHint 1: n % 10 הספרה האחרונה.n % 10 is the last digit.
הפתרון שלנוOur solution
main.py
def digit_sum(n):
    total = 0
    while n > 0:
        total += n % 10
        n //= 10
    return total

print(digit_sum(2026), digit_sum(0))
פלטOutput
10 0

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (25 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

שאלהQuestion 6

משולשA triangle

כתבו triangle(n) שמחזירה מחרוזת של n שורות: בשורה i (מ-1) יש i כוכביות, מיושרות לימין לרוחב n. למשל n=3: " *\n **\n***".

Write triangle(n) returning a string of n lines: line i (from 1) has i stars, right-aligned to width n. For n=3: " *\n **\n***".

איך הפלט אמור להיראות?What should the output look like?
   *
  **
 ***
****
main.py
רמזHint 1: " " * (n - i) + "*" * i." " * (n - i) + "*" * i.
הפתרון שלנוOur solution
main.py
def triangle(n):
    rows = []
    for i in range(1, n + 1):
        rows.append(" " * (n - i) + "*" * i)
    return "\n".join(rows)

print(triangle(4))
פלטOutput
   *
  **
 ***
****

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (25 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

שאלהQuestion 7

ניחוש עם מונהGuessing with a counter

המספר הסודי הוא 37. קראו ניחושים מהקלט עד שמנחשים נכון. אחרי כל ניחוש שגוי הדפיסו higher או lower. בסוף הדפיסו Got it in N tries. אם מגיעים ל-7 ניחושים בלי הצלחה, הדפיסו Out of tries ועצרו.

The secret number is 37. Read guesses from input until one is right. After each wrong guess print higher or lower. At the end print Got it in N tries. If you reach 7 guesses without success, print Out of tries and stop.

איך הפלט אמור להיראות?What should the output look like?
50
lower
25
higher
40
lower
37
Got it in 4 tries
main.py
רמזHint 1: while tries < 7:while tries < 7:
רמזHint 2: break כשצודקים.break when right.
הפתרון שלנוOur solution
main.py
SECRET = 37
tries = 0
while tries < 7:
    guess = int(input())
    tries += 1
    if guess == SECRET:
        print(f"Got it in {tries} tries")
        break
    print("higher" if guess < SECRET else "lower")
else:
    print("Out of tries")
פלטOutput
50
lower
25
higher
40
lower
37
Got it in 4 tries

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (30 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

נבנה על ידיBuilt by Ariel Yohimovich