לולאת while

להלן סדרת שאלות ותשובות. הן עוסקות בלולאת while שמספר סיבוביה אינו ידוע מראש ובלולאת while שמספר סיבוביה ידוע מראש. 

 

בקליטת קלט באמצעות הפונקציה input ההנחה היא שמוכנס קלט תקין, אלא אם כן נאמר אחרת.  

בשאלות המבקשות להשלים קוד יש להוסיף על הקוד ולא לשנות את הטקסט שכבר יש בו. 

הסימן ? משמעו ערך כלשהו.

בהצלחה!

 

152 * 173

לולאת while שמספר סיבוביה אינו ידוע מראש

לולאת ה-while בקוד זה כתובה באופן שגוי. מה השגיאה? הציעו תיקון. 

age = input(‘Insert age; -1 to stop: ‘)

while age != ‘-1’:

print(age)

age = input(‘Insert age; -1 to stop: ‘)

 

הוראות המופיעות בגוף של מבנה while ומתבצעות כל עוד תנאי הלולאה מתקיים צריכות להיות מוזזות ימינה ביחס לשורה הראשונה במבנה. הקוד התקין:

age = input(‘Insert age; -1 to stop: ‘)

while age != ‘-1’:

    print(age)

    age = input(‘Insert age; -1 to stop: ‘)

 

לולאת ה-while בקוד זה כתובה באופן שגוי. מה השגיאה? הציעו תיקון. 

grade = input(‘Insert grade; Q to stop: ‘)

while grade != ‘Q’

    print(grade + 50)

    grade = input(‘Insert grade; Q to stop: ‘)

בסוף השורה הראשונה במבנה while צריך להופיע סימן נקודתיים, וכאן הוא הושמט. הקוד התקין:

grade = input(‘Insert grade; Q to stop: ‘)

while grade != ‘Q’:

    print(grade + 50)

    grade = input(‘Insert grade; Q to stop: ‘)

הרצנו את הקוד הזה והמשתמשת הכניסה את המספר 9. מה הודפס? 

n = int(input(‘Please enter an integer bigger than 0: ‘))

while (n > 0):

     print(n)

     n = n – 1

9
8
7
6
5
4
3
2
1

הרצנו את הקוד הזה וההרצה הסתיימה באופן תקין. בסיומה יש ערך במשתנה psw. מהו?

correctPassword = ‘dani123’
psw = ”
psw = input(‘Please enter password: ‘)
while psw != correctPassword:
    psw = input(‘Please enter password: ‘)
print(‘Correct password.’)

תשובה: המחרוזת ‘dani123’. 

הרצנו את הקוד הזה וההרצה הסתיימה באופן תקין. בסיומה יש ערך במשתנה psw. מהו?

correctPassword = ‘dani123’
psw = ”
while psw != correctPassword:
    psw = input(‘Please enter password: ‘)
    if psw != correctPassword:
        print(‘Incorrect. Try again.’)
print(‘Correct password.’)

תשובה: המחרוזת ‘dani123’. 

הלולאה בקוד זה היא אינסופית – נכון או לא נכון?

import random
while (random.random() != 1.0):
    print(‘Am I eternal?’)

תשובה: נכון. המספר 1.0 הוא לא ערך החזרה של הפונקציה random.random ולכן תנאי הלולאה תמיד יתקיים.  

להלן קטע קוד חסר. השלימוהו באופן שהוא ידפיס 16 פעם אחת בלבד אם המשתמשת תכניס את המחרוזת ‘2’.

num = input(‘Please enter an integer: ‘)

while True:

     num = num * 2

print(num)

num = input(‘Please enter an integer: ‘)

while True:

     num = num * 2

     if num == 16:

         break

print(num)

תנו דוגמה לקלט שעבורו הלולאה הזאת תסתיים. 

while True:

     name = input(‘Your full name, please: ‘)

     if ‘ ‘ in name:             

           break

     print(‘Please enter both first name and last name.’)

תשובה: המחרוזת ‘Daniela Levy’. 

הרצנו את הקוד הזה. הוא הדפיס את המחרוזת ‘Dani’, ואחר כך, בשורה נפרדת, את המחרוזת ‘Dona’, ואחר כך, בשורה נפרדת, את המחרוזת ‘End’. מה הקלט שהכניסה המשתמשת? 

while True:

     name = input()

     if name == ‘Q’:

           break

     print(name)

print(‘End.’)

תשובה: המחרוזת ‘Dani’, אחר כך המחרוזת ‘Dona’, ולבסוף המחרוזת ‘Q’. 

השלימו את הקוד כך שלא יבצע את הוראת ה-print בגוף הלולאה אם הסיסמה המוכנסת היא המחרוזת ‘dani123’.

correctPassword = ‘dani123’

psw = ”

while psw != correctPassword:

    psw = input(‘Please enter password: ‘)

    print(‘Incorrect. Try again.’)

print(‘Correct password.’)

correctPassword = ‘dani123’
psw = ”
while psw != correctPassword:
    psw = input(‘Please enter password: ‘)
    if psw != correctPassword: 
       continue
    print(‘Incorrect. Try again.’)
print(‘Correct password.’)

קטע קוד זה צריך לקלוט מספר ולהודיע אם הוא ראשוני או אינו ראשוני. הקוד שגוי. תקנו אותו. התיקון צריך לחול על שתי שורות בלבד. 

n = int(input(‘Please enter an integer bigger than 2: ‘))

i = 2

s = ‘is a prime.’

while i < n:

   if n % i == 0:

      break

      s = ‘is not a prime.’

   i = i + 1

print(n, s)

n = int(input(‘Please enter an integer bigger than 2: ‘))

i = 2

s = ‘is a prime.’

while i < n:

   if n % i == 0:

      s = ‘is not a prime.’

      break

   i = i + 1

print(n, s)

לולאת while שמספר סיבוביה ידוע מראש

קטע קוד זה צריך לשוב ולהגדיל ב-1 את המספר שיש במשתנה x וגם להדפיס את המספר המוגדל, כל זה כל עוד הערך שיש ב-x לפני ההגדלה הוא 5. בקטע יש שגיאה. תקנוה. 

while x < 5:

x = x + 1

print(x)

while x < 5:

    x = x + 1

    print(x)

מה מדפיס קטע הקוד הזה:

i = 1

while i < 10:

     print(i)

     i = i + 3

1

4

7

מה מדפיס קטע הקוד הזה:

i = 1

while i > 10:

     print(i)

     i = i + 3

תשובה: הקוד אינו מדפיס דבר, כיוון שגוף הלולאה ובו הוראת ההדפסה אינו מתבצע ולו פעם אחת. 

שנו את קטע הקוד הזה באופן שהמספר 7 יודפס 20 פעמים.

i = 0

while i < 5:

     print(7)

i = 0

while i < 20:

     print(7)

     i = i + 1

שנו את קטע הקוד הזה באופן שידפיס את כל המספרים השלמים מ-1 עד 20 (כולל). .

i = 0

while i < 5:

     print(7)

i = 0

while i < 20:

     print(i + 1)

     i = i + 1

מה מדפיס קטע הקוד הזה: 

grade = 80

while grade < 90:

     grade = grade + 2

print(grade)

90

מה מדפיס קטע הקוד הזה: 

grade = 80

i = 0

while i < 5:

     grade = grade + 2

     i = i + 1

print(grade)

90

נתון קטע קוד זה. הסבירו מה הוא עושה. 

num = int(input(‘Please enter a positive integer: ‘))

mul = 1

count = 0

while (mul < num):

    count = count + 1

    mul = mul * count   

print(count – 1)

תשובה: קטע הקוד מוצא כמה מספרים שלמים חיוביים מכפלתם קטנה ממש ממספר שלם שנקלט.

נתון קטע קוד חסר. השלימוהו באופן שידפיס 5 פעמים את המחרוזת ‘Europe’, כל פעם בשורה נפרדת. 

i =

while i > 0

     print(‘Europe’)

i = 5

while i > 0:

     print(‘Europe’)

     i = i – 1

נתון קוד זה:

print(‘I have to say it again’, end=”)

i = 0

while :

    print(‘, and again’, end=”)

print(‘.’)

השלימו באופן שידפיס בשורה אחת בלבד את המחרוזת הזאת: 

I have to say it again, and again, and again, and again, and again, and again.

print(‘I have to say it again’, end=”)

i = 0

while i < 5:

    print(‘, and again’, end=”)

    i = i + 1

print(‘.’)

השלימו את הקוד כך שידפיס את כל המספרים האי זוגיים בין 1 ל-20. אין לשנות את ההזחה של הוראת ההדפסה.

i = 1

while i < 20:

     print(i)

i = 1

while i < 20:

     if i %% 2 == 0:

           continue 

     print(i)