פייתון/פייתון גרסה 3/קלט מהמשתמש: הבדלים בין גרסאות בדף

תוכן שנמחק תוכן שנוסף
אין תקציר עריכה
שורה 54:
 
פעולת המרה של טיפוסים מסוג אחד לאחר נקראת Casting.
 
==תרגיל==
{{תרגיל
|מספר=1
|שאלה=
צרו משוואת בשני נעלמים מהצורה
<math>
\begin{cases}
ax + by = c\\
dx + ey =f
\end{cases}
</math>
 
# כתבו קוד לפיו המשתמש יזין את ששת המשתנים <math>a,b,c,d,e,f</math>
# הגדירו את הנעלמים <math>x</math> ו-<math>y</math> כך שקטע הקוד יחשב את שני הנעלמים על פי המספרים אותם המשתמש יזין. לדוגמה אם המשתמש יקליד את המשתנים <math>1,2,3,4,5,6</math> תתקבל התוצאה עבור הנעלמים.
# לבסוף הגדירו פקודת הדפס ובה יהיה רשום <math>\text{the results (x,y) are (x) and (y)}</math>
|פתרון=
תחילה נגדיר את המשתנים:
<source lang = "python">
num_1 = float(input('Insert a number1: '))
num_2 = float(input('Insert a number2: '))
num_3 = float(input('Insert a number3: '))
num_4 = float(input('Insert a number4: '))
num_5 = float(input('Insert a number:5 '))
num_6 = float(input('Insert a number6: '))
</source>
 
מאחר שאנו רוצים כי המחשב יחשב את הנעלמים איננו יכולים להעזר בהם בעת הגדרת המשתנה. עלינו להיפטר מהם.
<math>
\begin{cases}
ax + by = c\\
dx + ey =f\\
\end{cases}
</math>
 
על כן נפתור בעצמנו את המשוואות:
 
<math>
\begin{cases}
y=\frac{c-ax}{b}\\
y=\frac{f-dx}{e}
\end{cases}
</math>
 
<math>
\begin{cases}
x=\frac{c-by}{a}\\
x=\frac{f-ey}{d}\\
\end{cases}
</math>
 
עדין נותר לנו משתנה. נפתור את המשואות:
<math>
\begin{cases}
\frac{f-dx}{e}=\frac{c-ax}{b}\\
\frac{f-ey}{d}=\frac{c-by}{a}
\end{cases}
</math>
 
נשם לב כי בכדי למצוא את הנעלם x<math>x</math> אנחנו בעצם משווים בין שתי משוואות עם נעלם <math>y</math> ולכן:
 
הנעלם <math>y</math>
<math>
\begin{cases}
b(f-dx)=(e)(c-ax)\\
bf-bdx=ec-eax
eax-bdx=ec-bd
x(ea-bd)=ec-bd
x=\frac{ec-bd}{ea-bd}
\end{cases}
</math>
 
הנעלם <math>x</math>
<math>
\begin{cases}
a(f-ey)=d(c-by)
af-aey=dc-bdy
bdy-aey=dc-af
y(bd-ae)=dc-af
y=\frac{bd-ae}{dc-af}
\end{cases}
</math>
 
עתה נציב את הנעלמים באמצעות המשתנים:
<source lang = "python">
y=((num_5*num_3-num_2*num_4)/(num_5*num_1-num_4*num_2))
x=((num_2*num_4-num_1*num_6)/(num_4*num_3+num_1*num_6))
 
</source>
 
נגדיר את פעולת ההדפס. יתכן כי בחלק זה תסתבכו מפני שלא הוסבר כיצד להציג סוגרים. ניתן לראות הסבר לכל הפעולות של ההדפס בדף [[פייתון/פייתון גרסה 3/פקודת הדפס|זה]]
<source lang = "python">
print('the results of (x,y) are', (x), 'and', (y))
</source>
|יישור=ימין}}
 
==מקורות חיצוניים==