Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 61
Image: ubuntu2204
Kernel: Python 3 (Anaconda 2022)

לולאת while


צפו בסרטון שלמטה וענו על השאלות והתרגילים שבהמשך:
from IPython.display import YouTubeVideo YouTubeVideo("yT-iEiwbxiI")
תרגיל 1
עבור כל אחד מקטעי הקוד שבהמשך, מה ידפיס קטע הקוד? קודם ענו מבלי להריץ את הקוד ולאחר מכו העתיקו את הקוד ובדקו את תשובתכם.
c = 1 product = 1 while c <= 5: product = product * c c = c + 1 print(product) ### 120
x = 7 s = 0 while x > 0: s = s + x x = x - 1 x = s print('x=',x) ### x=28
x = 2 n = 0 while n < 10: x = x + 2 * n n = n + 1 print('x=',x) ### 92 (התחלתי x= 1 תשובה ראשונה שלי הייתה 87 כיוון שבטעות חישבתי עם)
xx
x = 1 total = 0 while x <= 10: total = total + x x = x + 1 print('x=',2 * x) ### x= 22, (Total = 55)
תרגיל 2
,כתבו קטע קוד, שבעזרת לולאות while ידפיס תמונה כדוגמת זו שלמטה.

pic1

x = 1 while x <= 10: if x == 1 or x == 10: for i in range(20): print('*', end = '') print('*') else: for i in range(19): if i == 0: print('*',end='') print(' ',end='') print('*') x +=1
********************* * * * * * * * * * * * * * * * * *********************
תרגיל 3
בדקו באינטרנט מה היא השערת קולץ וכיתבו פונקציה בשם Collatz המקבלת מספר שלם ומחזירה את מספר הפעמים שיש להפעיל עליו את הכלל, כדי להגיע ל- 1. הפעילו את הפונקציה על ערכי כניסה שונים.
def coltz(num): steps = 0 while num != 1: if num % 2 == 0: num /= 2 else: num == (num*3)+1 steps += 1 return steps steps = coltz(4) print(f'it took {steps} steps to get to one')
it took 2 steps to get to one
בעזרת ללאת while נוכל לשרטט גרף של פונקציה מסימת. נראה זאת באמצעות הדוגמא שבהמשך. נניח כל רוצים לשרטט גרף של הפונקציה :
בתחום . קטע הקוד שבהמשך עושה זאת:
import matplotlib.pyplot as plt def f(x): return -x**2 - 3 * x + 4 dx = 0.02 x= -2 while x <= 2: plt.plot(x,f(x),'g.') x = x + dx plt.xlabel('x',fontsize=16, color='red') plt.ylabel('y',fontsize=16, color='red') plt.title('$f(x)=-x^2-3x+4$',fontsize=24,color='m') plt.grid(True)
Image in a Jupyter notebook
תרגיל 4:כתבו פונקציה המקבלת כפרמטרים שם של פונקציה, גבול עליון וגבול תחתון ומשרטטת גרף של הפונקציה עבור 100 נקודות שונות בתחום.
f = lambda x: -x**2 -3*x +4 def function_plotter(function, limit_top, limit_bottom): dx = (abs(limit_top) + abs(limit_bottom))/100 while limit_bottom <= limit_top: plt.plot(limit_bottom,function(limit_bottom),'g.') limit_bottom += dx plt.xlabel('x',fontsize=16, color='red') plt.ylabel('y',fontsize=16, color='red') plt.title('function',fontsize=24,color='m') plt.grid(True) function_plotter(f,2,-2)
Image in a Jupyter notebook
תרגיל 5: שנו את הפונקציה שכתבתם בתרגיל הקודם כך שנקודות עבורם ערך ה-y של המשתנה התלוי הן חיוביות ישורטטו בצבע אדום ואילו נקודות שעבורן y שלילי ישורטטו בצבע כחול.
f = lambda x: -x**2 -3*x +4 def function_plotter(f1, lt, lb): dx = (abs(lt) + abs(lb))/100 while lb <= lt: y = f1(lb) if y < 0: plt.plot(lb,y,'r.') else: plt.plot(lb,y,'b.') lb += dx plt.xlabel('x',fontsize=16, color='red') plt.ylabel('y',fontsize=16, color='red') plt.title('function',fontsize=24,color='m') plt.grid(True) function_plotter(f,10,-10)
Image in a Jupyter notebook
תרגיל 6: שנו את הפונקציה אותה כתבתם כך, שבגרף היא תסמן את נקודות המקסימום המינימום ואת האפסים של הפונקציה המשורטטת. הקפידו לצבוע כל סוג של נקודות בצבע שונה.
import matplotlib.pyplot as plt f = lambda x: x**2 - 4 def function_plotter(f1, lt, lb): dx = (abs(lt) + abs(lb))/500 lst_y = [] lst_x = [] while lb <= lt: lst_y.append(f1(lb)) lst_x.append(lb) lb += dx for i in range(len(lst_x)): if lst_y[i] > 0 and lst_y[i] < 0.2: plt.plot(lst_x[i],lst_y[i],'gd') elif lst_y[i] == min(lst_y): plt.plot(lst_x[i],lst_y[i],'gd') else: plt.plot(lst_x[i],lst_y[i],'r.') plt.xlabel('x',fontsize=16, color='red') plt.ylabel('y',fontsize=16, color='red') plt.title('function',fontsize=24,color='m') plt.grid(True) function_plotter(f,10,-10)
Image in a Jupyter notebook
תרגיל 7 כתבו תוכנית היצרת ספירלה כדוגמת זו שבתמונה למטה

import matplotlib.pyplot as plt import numpy as mt plt.axes(projection = 'polar') r = 1 angles = mt.arange(0, (20 * mt.pi), 0.01) for i in angles: plt.polar(i, r, 'g.') r *= 1.001
Image in a Jupyter notebook
תרגיל 8:כתבו תוכנית המשרטטת את הצורה שבהמשך

import matplotlib.pyplot as plt x = 10 y = 10 z = 0.25 plt.figure(figsize=(20,20)) while x >= 0.25: plt.plot([x,-x],[y,y],'g-') plt.plot([-x,x],[-y,-y],'m-') plt.plot([x,x],[y,-y],'b-') plt.plot([-x,-x],[y,-y],'r-') x -= abs(z) y -= abs(z) z -= 0.0033 plt.grid(False)
Image in a Jupyter notebook
בעיית אתגר: הציור מתאר אונייה השטה ימינה במהירות קבועה שגודלה 20 קמ"ש. מהנקודה A נורה טורפדו לכיוון האונייה. הטורפדו נע במהירות קבועה שגודלה 50 קמ"ש. חרטום הטורפדו מופנה תמיד לכיוון האונייה. כעבור כמה זמן ובאיזה מקום יפגע הטורפדו באונייה? רשמו קוד הפותר את הבעיה, שרטטו את מסלול הטורפדו על מערכת צירים.

pic_4