לולאותLoops
while, for, range, break ו-continue, ולולאת תפריט.
while, for, range, break and continue, and a menu loop.
לוח הכפל של מספרTimes table for a number
תזכורת: מה ביקשוReminder: the task
קלטו מספר והדפיסו את לוח הכפל שלו מ-1 עד 10, בפורמט 3 x 4 = 12.
Read a number and print its times table from 1 to 10, as 3 x 4 = 12.
הפתרון המלא והפלט שלוFull solution and its output
n = int(input())
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")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
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
סכום עד אפסSum until zero
תזכורת: מה ביקשוReminder: the task
קלטו מספרים עד שמקלידים 0, והדפיסו את הסכום שלהם.
Read numbers until 0 is typed, then print their sum.
הפתרון המלא והפלט שלוFull solution and its output
total = 0
while True:
n = int(input())
if n == 0:
break
total += n
print(total)10 20 30 0 60
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ספירה לאחורCountdown
תזכורת: מה ביקשוReminder: the task
קלטו מספר והדפיסו ספירה לאחור עד 1 בשורה אחת עם רווחים, ואחריה 🚀.
Read a number and print a countdown to 1 on one line with spaces, then 🚀.
הפתרון המלא והפלט שלוFull solution and its output
n = int(input())
for i in range(n, 0, -1):
print(i, end=" ")
print("🚀")5 5 4 3 2 1 🚀
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ניחוש מספרGuess the number
תזכורת: מה ביקשוReminder: the task
המספר הסודי הוא 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.
הפתרון המלא והפלט שלוFull solution and its output
secret = 42
tries = 0
while True:
guess = int(input())
tries += 1
if guess == secret:
break
elif guess < secret:
print("higher")
else:
print("lower")
print(f"got it in {tries} tries")10 higher 50 lower 42 got it in 3 tries
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 6: תפריט ראשיShelf, step 6: the main menu
תזכורת: מה ביקשוReminder: the task
בנו תפריט שחוזר: 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: ?.
הפתרון המלא והפלט שלוFull solution and its output
count = 0
while True:
choice = input("> ")
if choice == "1":
title = input("title: ")
count += 1
print(f"added: {title}")
elif choice == "2":
print(count)
elif choice == "q":
print("bye")
break
else:
print("?")> 1 title: Dune added: Dune > 1 title: Emma added: Emma > 2 2 > x ? > q bye
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
FizzBuzzFizzBuzz
תזכורת: מה ביקשוReminder: the task
הדפיסו את המספרים 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!)
הפתרון המלא והפלט שלוFull solution and its output
for i in range(1, 16):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
משולש כוכביותStar triangle
תזכורת: מה ביקשוReminder: the task
קלטו גובה והדפיסו משולש ממורכז מכוכביות. לגובה 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.
הפתרון המלא והפלט שלוFull solution and its output
h = int(input())
for i in range(1, h + 1):
print(" " * (h - i) + "*" * (2 * i - 1))4 * *** ***** *******
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.