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

PythonPython
6
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

לולאותLoops

while, for, range, break ו-continue, ולולאת תפריט.

while, for, range, break and continue, and a menu loop.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. כמה פעמים ירוץ for i in range(2, 8, 2):?How many times does for i in range(2, 8, 2): run?

2. מה ידפיס הקוד?What does this print?
python
for n in [1, 2, 3, 4]:
    if n == 3:
        continue
    print(n, end=" ")

3. מתי לולאת while נעצרת?When does a while loop stop?

תרגיליםExercises

תרגילExercise 1

לוח הכפל של מספרTimes table for a number

קלטו מספר והדפיסו את לוח הכפל שלו מ-1 עד 10, בפורמט 3 x 4 = 12.

Read a number and print its times table from 1 to 10, as 3 x 4 = 12.

איך הפלט אמור להיראות?What should the output look like?
7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
main.py
רמזHint 1: for i in range(1, 11):for i in range(1, 11):
רמזHint 2: print(f"{n} x {i} = {n * i}")print(f"{n} x {i} = {n * i}")
לפתרוןSolution
תרגילExercise 2

סכום עד אפסSum until zero

קלטו מספרים עד שמקלידים 0, והדפיסו את הסכום שלהם.

Read numbers until 0 is typed, then print their sum.

איך הפלט אמור להיראות?What should the output look like?
10
20
30
0
60
main.py
רמזHint 1: while True: ו-break כשמגיע 0.while True: and break on 0.
רמזHint 2: צובר total = 0 לפני הלולאה.An accumulator total = 0 before the loop.
לפתרוןSolution
תרגילExercise 3

ספירה לאחורCountdown

קלטו מספר והדפיסו ספירה לאחור עד 1 בשורה אחת עם רווחים, ואחריה 🚀.

Read a number and print a countdown to 1 on one line with spaces, then 🚀.

איך הפלט אמור להיראות?What should the output look like?
5
5 4 3 2 1 🚀
main.py
רמזHint 1: range(n, 0, -1)range(n, 0, -1)
רמזHint 2: end=" "end=" "
לפתרוןSolution
תרגילExercise 4

ניחוש מספרGuess the number

המספר הסודי הוא 42. קלטו ניחושים עד שהמשתמש מצליח. אחרי כל ניחוש שגוי הדפיסו higher או lower, ובסוף got it in N tries.

The secret is 42. Read guesses until the user gets it. After each wrong guess print higher or lower, and finally got it in N tries.

איך הפלט אמור להיראות?What should the output look like?
10
higher
50
lower
42
got it in 3 tries
main.py
רמזHint 1: מונה ניסיונות שגדל בכל סיבוב.A tries counter that grows each pass.
רמזHint 2: while True עם break כשמנחשים נכון.while True with break on a correct guess.
לפתרוןSolution
תרגילExercise 5

מדף, צעד 6: תפריט ראשיShelf, step 6: the main menu

פרויקט מלווהCompanion project

בנו תפריט שחוזר: 1 מוסיף ספר (קולט שם ומדפיס added: <שם>), 2 מדפיס כמה ספרים נוספו, q יוצא ומדפיס bye. כל בחירה אחרת: ?.

Build a repeating menu: 1 adds a book (reads a title and prints added: <title>), 2 prints how many were added, q quits and prints bye. Anything else: ?.

איך הפלט אמור להיראות?What should the output look like?
> 1
title: Dune
added: Dune
> 1
title: Emma
added: Emma
> 2
2
> x
?
> q
bye
shelf.py
רמזHint 1: מונה count = 0 לפני הלולאה.A counter count = 0 before the loop.
רמזHint 2: while True + if/elif/else + break.while True + if/elif/else + break.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

FizzBuzzFizzBuzz

הדפיסו את המספרים 1 עד 15, אבל במקום כפולות של 3 הדפיסו Fizz, במקום כפולות של 5 Buzz, ובמקום כפולות של שניהם FizzBuzz. (שאלת ראיון קלאסית!)

Print 1 to 15, but print Fizz instead of multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both. (A classic interview question!)

איך הפלט אמור להיראות?What should the output look like?
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
main.py
רמזHint 1: בודקים קודם את 15 (שני התנאים).Check 15 (both) first.
רמזHint 2: i % 3 == 0i % 3 == 0
לפתרוןSolution
אתגרChallenge 2

משולש כוכביותStar triangle

קלטו גובה והדפיסו משולש ממורכז מכוכביות. לגובה 3 השורות הן: שני רווחים וכוכבית, רווח ושלוש כוכביות, חמש כוכביות.

Read a height and print a centred star triangle. For height 3 the rows are: two spaces and a star, a space and three stars, five stars.

איך הפלט אמור להיראות?What should the output look like?
4
   *
  ***
 *****
*******
main.py
רמזHint 1: בשורה i (מ-1): 2*i-1 כוכביות.Row i (from 1): 2*i-1 stars.
רמזHint 2: לפניהן height-i רווחים.Before them, height-i spaces.
לפתרוןSolution

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