Introduction to Programming with Python Code එක මගින් සංකීර්ණ තීරණ ගැනීම Dileepa S. Rajapaksa | http://www.windowsgeek.lk @dsrajapaksa Microsoft Virtual Academy
කලින් Video එකේ අපි කතා කලේ මොනවාද? If condition භාවිතයෙන් තීරණ ගැනීම Coding ලිවීමේදී indentation තැබීම If Conditions භාවිතයෙන් සංඛ්යා සංසන්ඳනය Else conditions භාවිතය Boolean Variables භාවිතා කර ඒවා If conditions සමඟ යෙදීම Microsoft Virtual Academy
Demo කලින් Video එකේ අභියෝගයට පිළිතුරු Microsoft Virtual Academy
Code එකෙන් සංකීර්ණ තීරණ ගැනීම and/or, nested if, elif Last part Practice your language Microsoft Virtual Academy
සමහර වෙලාවට අවසාන තීරණයට බලපාන condition ගොඩක් තියෙන්න පුළුවන්. ඔබ ඉන්නේ එංගලන්තයේ නම් Hello කියන්න, ස්පාඤ්ඤයේ නම් Hola කියන්න, ප්රංශයේනම් bonjour කියන්න. ඔබට lottery එකක් ඇදුනොත් හා එහි ත්යාගය ඩොලර් මිලියනයට වැඩිනම් සුඛෝපභෝගී ජිවිතයක් ගත කරන්න. අද සඳුදා නම් පරීක්ෂා කර බලන්න නැවුම් කෝපි තියෙනවද නැතිද බව. නැතිනම් ලඟම තියෙන cafe එකට යන්න Microsoft Virtual Academy
If, then, otherwise… Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
ඔබ ඉන්නේ කැනඩාවේ නම් Hello කියන්න, ස්පාඤ්ඤයේ නම් Hola කියන්න, ප්රංශයේනම් bonjour කියන්න. මේක පොඩ්ඩක් වැදගත් අවස්ථාවක්.ඔයාලට එක Condition එකයි පරීක්ෂාකරන්න තියෙන්නේ.ඒත් ඒ එක Condition එකට විවිධ අගයන් තියෙන්න පුළුවන්. Microsoft Virtual Academy
වෙනස් අගයන් පරීක්ෂා කිරීමට “elif” මගින් පුළුවන්. country = input("Where are you from? " ) if country == "CANADA" : print("Hello") elif country == “SPAIN" : print(“Hola") elif country == "FRANCE" : print("Bonjour") බලන්න, elif statement එක indent වෙලා නැහැ !. “elif” කියන්නේ else if කියන එක කෙටියෙන්. Microsoft Virtual Academy
මොකද වෙන්නේ Japan හෝ Italy ඇතුලත් කලොත්. අපි දක්වා නැති conditions පරීක්ෂා කිරීමට අපි else statement එකක් යෙදිය යුතුයි. country = input("Where are you from? " ) if country == "CANADA" : print("Hello") elif country == “SPAIN" : print(“Hola") elif country == "FRANCE" : print("Bonjour") else : print("Aloha/Ciao/G’Day") Microsoft Virtual Academy
Demo elif Last part Practice your language Microsoft Virtual Academy
Conditions කිහිපයක් සම්බන්ධ කිරීම Last part Practice your language Microsoft Virtual Academy
ඔබට lottery එකක් ඇදුනොත් සහ ත්යාගය මිලියනයට වැඩිනම් ඔබට lottery එකක් ඇදුනොත් සහ ත්යාගය මිලියනයට වැඩිනම්.විශ්රාම ගිහින් luxury ජීවිතයක් ගෙවන්න. සමහර වෙලාවට ඊළඟ පියවර කරනවාද නැද්ද කියන එක සාධක ගොඩක් මත රඳාපවතිනවා. මම Lottery එකක් දින්නොත්, ඒත් දීනේ ඩොලර් 5යි නම් , විශ්රාම ගන්න බැහැ. Lottery එකේ ත්යාගය ඩොලර් මිලියනයයි එහෙත් මම දිනලා නැත්නම් , මට විශ්රාම ගන්න බැහැ. මට විශ්රාම ගන්න පුළුවන් මම Lottery එක දිනලා නම් සහ ( and ) ත්යාගය ඩොලර් මිලියනයක් නම් පමණයි. Microsoft Virtual Academy
ඔබ “and” භාවිතා කරනවිට ඔබ කියන්නේ සියළුම conditions සත්ය වියයුතු බව. Microsoft Virtual Academy
AND වලින් ලැබෙන පිළිතුර සත්ය යැයි සලකන්නේ අවස්තා දෙකම සත්ය වූ විට පමණි #Imagine you have code that ran earlier which #set these two variables wonLottery = True bigWin = True #print statement only executes if both conditions are true if wonLottery and bigWin : print("you can retire") Microsoft Virtual Academy
AND භාවිතයේ විය හැකි සියළුම අවස්ථා if firstCondition and secondCondition : First Condition is Second Condition is Statement is True False Microsoft Virtual Academy
Demo and Last part Practice your language Microsoft Virtual Academy
සමහර අවස්ථා වල අපිට සිදුවෙනවා එක් සිද්ධියක් පමණක් සත්ය වූ විට යමක් කරන්න. දිනය සෙනසුරාදා හෝ ( or ) ඉරීදානම් මට නිදාගන්න පුළුවන්. වහිනවානම් හෝ ( or ) අහස කළුකරලා නම් Bike එකේ වැඩට යන්න එපා. Microsoft Virtual Academy
ඔබ “or” භාවිතා කරනවා නම් එයින් කියන්නේ condition එකක් හෝ සත්යනම් අදාල කොටස් කරන්න කියන එකයි. Microsoft Virtual Academy
OR වලින් ලැබෙන පිළිතුර සත්යවන්නේ දෙපසින් එක් හෝ අවස්තාවක් තෘප්ත වනවානම් පමණි. අවස්තා දෙකම සත්යවීම අනිවාර්යය නොවේ. #Imagine you have code that ran earlier which #set these two variables saturday = True sunday = False #print statement executes if either condition is true if saturday or sunday : print("you can sleep in") Microsoft Virtual Academy
විය හැකි සියළුම අවස්ථා if firstCondition or secondCondition : First Condition is Second Condition is Statement is True False Microsoft Virtual Academy
Demo or Last part Practice your language Microsoft Virtual Academy
ඔයාට එක if statement එකක් ඇතුලේ “and”සහ “or” කිහිපයක් භාවිතාකරන්න පුළුවන් if month == "Sep" or month =="Apr" \ or month == "Jun" or month == "Nov" : print("There are 30 days in this month") if favMovie == "Star Wars" \ and favBook == "Lord of the Rings" \ and favEvent == "ComiCon" : print("You and I should hang out") Microsoft Virtual Academy
This one doesn’t seem to work the way you would expect! ඔයාට එක statement එකක් ඇතුලේ “and” සහ “or” කිහිපයක් සම්බඳ කරන්න පුළුවන් if country == "CANADA" and \ pet == "MOOSE" or pet == "BEAVER" : print("Do you play hockey too?") මේ වෙනස් අවස්ථා සියල්ලම සලකා බලන්න Country = CANADA, Pet = MOOSE Country = CANADA, Pet = BEAVER Country = VIETNAM, Pet = MOOSE Country = VIETNAM, Pet = BEAVER This one doesn’t seem to work the way you would expect! Microsoft Virtual Academy
Demo and/or සම්බන්ධ කිරීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
ගණිත කර්ම කිරීමේ අනුපිළිවල පාසලේදී ඉගෙනගත්තා මතකද? 8+5*2=? වැඩිකිරීම හා බෙදීම , එකතුකිරීම හා අඩුකිරීමට කලින් සිදුවෙනවා. 8+5*2 = 18 Microsoft Virtual Academy
“and”/”or” ක්රියාත්මක වීමටත් පිළිවලක් තියෙනවා “and” මුලින් සලකා බලනවා. if country == "CANADA" and pet == "MOOSE" \ or pet == "BEAVER" : print("Do you play hockey too?") Microsoft Virtual Academy
ගණිතයේදී කොහොමද වැඩි කිරීමට කලින් එකතු කිරීම කරගන්නේ? වරහන් භාවිතයෙන්! (8+5)*2 = 26 Microsoft Virtual Academy
“and” වලට කලින් “or” ක්රියාත්මක කිරීම සඳහා වරහන් භාවිතා කරන්න පුළුවන්. if country == "CANADA" and \ (pet == "MOOSE" or pet == "BEAVER") : print("Do you play hockey too") එකම statement එකක and/or දෙකම භාවිතා කරලා තියෙන වෙලාවක ප්රතිඵලය ගැන සැකයක් තියෙනවානම් වරහන් යොදන්න. එ විට Code එක කියවීමට පහසු වනු ඇති අතර වැරදි සිදුවීමේ ඉඩ ප්රස්ථාව අඩුවනු ඇත… Microsoft Virtual Academy
සමහර වෙලාවන් වල conditions කිහිපයක් පරීක්ෂා කිරීමට “and”/”or” පමණක් භාවිතයෙන් වැඩක් වෙන්නේ නැහැ. මේ ප්රශ්ණය code එකේන් මගහැරගන්නේ කොහොමද? සඳුදා නම් කෝපි තියනවාදැයි බලන්න. කෝපි නැත්නම් ළඟම තියෙන cafe එකට යන්න. මෙම අවස්ථාවේදී ඔබට එක condition එකක් පරීක්ෂා කරන්න වෙනවා.ඒක සත්යනම් ඔබට තවත් condition එකක් පරීක්ෂා කරලා බලන්න වෙනවා. Microsoft Virtual Academy
Nested if statements Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
ඔයාලට if statement එකක් ඇතුලේ එකක් nest කරන්න පුළුවන් monday = True freshCoffee = False if monday : #you could have code here to check for fresh coffee # the if statement is nested, so this if statement # is only executed if the other if statement is true if not freshCoffee : print("go buy a coffee!") print("I hate Mondays") print("now you can start work") ඔබ සැලකිලිමත් වියයුතුයි code එක indent වෙලා තියෙන විදිහ පිළිබඳව ඒ indent කිරීම මගින් එක් එක් if statement එකට අදාල code එක තීරණය වෙනවා. Microsoft Virtual Academy
Demo Nested if statements Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
ඔබේ අභියෝගය… ශ්රී ලංකාවේ online store එකකින් ඇණවුම් කරපු දේකට මුළු වියදම ගණනය කරන්න. ඇණවුමේ මුළු වටිනාකම සහ user ගේ රට පිළිව්බඳ විස්තර ... ගෙන් ලබාගන්න. User ශ්රී ලංකාවේ නම් පළාත කුමක්ද? user ගෙන් ලබාගන්න. ඇණවුම ශ්රී ලංකාව පිටතින් නම් කිසිඳු බද්ධක් අය නොකර සිටින්න. ඇණවුම ශ්රී ලංකාවේ නම් බදු පහත පරිදි ගණනය කරන්න. බස්නාහිර පළාත 2% General sales Tax (GST) දකුණු පළාත, නැගෙනහිර පළාත, මධ්යම පළාත් වලට අයකිරීම 4% වශයෙන් අය කරන්න. අනෙකුත් සෑම පලාතක් සඳහාම 5%ක් සහ 1% GST වශයෙන් එකතුකරන්න. බදු සමග එකතුව userට ඉදිරිපත් කරන්න. Microsoft Virtual Academy
Code එක පරීක්ෂාකිරීම… Code එක හරියටම වැඩ කරනවාද බලාගන්නේ කොහොමද? ශ්රී ලංකාවෙන් පිට කෙනෙකුට (බදු නැත ) බස්නාහිර පළාතේ කෙනෙකුට (බදු 2%) දකුණු පළාත, නැගෙනහිර පළාත, මධ්යම පළාත් වල කෙනෙකුට (බදු 4%) අනෙකුත් සෑම පලාතක කෙනෙක් සඳහා ( උදා: උතුරු මැදපළාත) (බදු 6%) Microsoft Virtual Academy
සාරාංශය… If condition භාවිතයෙන් සංකීර්ණ තීරණ ගැනීම elif භාවිතා කර අනෙකුත් අවස්තාවන් හිදී තීරණ ගැනීම විවිධ අවස්ථාවන් හිදී Conditions කීපයක් සැසදීම AND , OR භාවිතය Nested if statements භාවිතා කිරීම Microsoft Virtual Academy
සුභ පැතුම් ! ඔබට දැන් ඉතා සංකීර්ණ තීරණ ගැනීමට සුදුසු වැඩසටහනක් නිර්මාණය කරන්න පුලුවන්. Microsoft Virtual Academy