20/11/20171MAKINGDECISIONSConditionalsControl flow■ A program’s control flow is the order in which the program’scode executes.■ The control flow of a Python program depends on conditionalstatements, loops, and function calls.20/11/20172The if Statement■ When you need to execute some statements only when somecondition holds, or choose statements to execute depending onmutually exclusive conditions.■ The compound statement if —comprising … Continue reading “control flow of a Python program | My Assignment Tutor”
20/11/20171MAKINGDECISIONSConditionalsControl flow■ A program’s control flow is the order in which the program’scode executes.■ The control flow of a Python program depends on conditionalstatements, loops, and function calls.20/11/20172The if Statement■ When you need to execute some statements only when somecondition holds, or choose statements to execute depending onmutually exclusive conditions.■ The compound statement if —comprising if, elif, and else clauses—letsyou conditionally execute blocks of statements.ConditionF Telse block if blockThe if Statement■ An if statement is made up of the if keyword, followed by acondition and a colon (:), as in if expression:• The elif and else clauses areoptional.• Note that, unlike somelanguages, Python does nothave a “switch” statement.• Use if, elif, and else for allconditional processing.20/11/20173Block Statements■ A block of code is a grouped set of programming statements.■ Each line in a block must have the same number of spaces forindentation■ We group statements together into blocks because they are related.■ The statements need to be run together.The if Statement & block indentation• Use consistent spacing to make your code easier to read. If youstart writing a program and put four spaces at the beginning of ablock, keep using four spaces at the beginning of the other blocksin your program.• Be sure to indent each line in the same block with the samenumber of spaces.20/11/20174Combining conditionals■ You can combine conditions by using the keywords and and or, whichproduces shorter and simpler code.Try this out…(1)■ Create a program that reads a letter of the alphabet from the user. Ifthe user enters a, e, i, o or u then your program should display amessage indicating that the entered letter is a vowel. If the userenters y then your program should display a message indicating thatsometimes y is a vowel, and sometimes y is a consonant. Otherwiseyour program should display a message indicating that the letter isa consonant.20/11/20175Try this out…(2)■ The length of a month varies from 28 to 31 days. In this exercise youwill create a program that reads the name of a month from the useras a string. Then your program should display the number of days inthat month. Display “28 or 29 days” for February so that leap yearsare addressed.Try this out…(3)■ A particular mobile phone plan includes 500 minutes and 1000 textmessages for £15.00 a month. Each additional minute of air timecosts 25p, while additional text messages cost 15p each. All cellphone bills include an additional charge of 44p to support 999 callcentres, and the entire bill (including the 999 charge) is subject to 5percent sales tax.■ Write a program that reads the number of minutes and textmessages used in a month from the user. Display the base charge,additional minutes charge (if any), additional text message charge (ifany), the 999 fee, tax and total bill amount. Only display theadditional minute and text message charges if the user incurredcosts in these categories. Ensure that all of the charges are displayedusing 2 decimal places.20/11/20176Try this out…(4)■ Most years have 365 days. However, the time required for the Earth toorbit the Sun is actually slightly more than that. As a result, an extraday, February 29, is included in some years to correct for thisdifference. Such years are referred to as leap years.■ The rules for determining whether or not a year is a leap year follows:– Any year that is divisible by 400 is a leap year.– Of the remaining years, any year that is divisible by 100 is not aleap year.– Of the remaining years, any year that is divisible by 4 is a leapyear.– All other years are not leap years.■ Write a program that reads a year from the user and displays amessage indicating whether or not it is a leap year.NEXT TOPIC…Loops